ActiveMQ在SpringMVC下配置和应用


ActiveMQ在SpringMVC下配置和应用

ActiveMQ下载地址

http://activemq.apache.org/download-archives.html

解压,启动
./bin/activemq start

访问Web控制台

默认用户名和密码:admin:admin
http://192.168.9.10:8161/

设置Web控制台用户名和密码
vim ./conf/users.properties

设置访问端口

默认是61616
vim ./conf/activemq.xml

加入Maven

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.26</version>
</dependency>

在Web控制台创建一个队列(Queue),如myFirstQueue

测试代码:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.9.10:61616");

try {

Connection conn = connectionFactory.createConnection();
conn.start();

Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createQueue("myFirstQueue");

MessageProducer producer = session.createProducer(dest);

JSONObject json = new JSONObject();
json.put("id", "101");
json.put("name","book");

TextMessage msg = session.createTextMessage(json.toJSONString());
producer.send(msg);

session.commit();
session.close();
conn.close();

}catch(JMSException e) {
e.printStackTrace();
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注