ActiveMq 简单使用
2017-11-08 12:03
344 查看
一. 生产者(消息发送者)
1.创建连接
private String userName = “”;
private String password = “”;
private String brokerURL = “tcp://127.0.0.1:61616”;
2. connection的工厂
private ConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, brokerURL);
3. 连接对象
private Connection connection = factory.createConnection();
connection.start();
4. 会话
private Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//具体参数自己定
5. Destination
private Destination destination = session.createQueue(“text-msg”);//创建名为text-msg的队列
6. 生产者(消息发送者
private MessageProducer producer = session.createProducer(destination);
7. 发送消息
TextMessage textMsg = session.createTextMessage(“发送消息”);
producer.send(textMsg);
二. 消费者(消息接收者)
1.创建连接
private String userName = “”;
private String password = “”;
private String brokerURL = “tcp://127.0.0.1:61616”;
2. connection的工厂
private ConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, brokerURL);
3. 连接对象
private Connection connection = factory.createConnection();
connection.start();
4. 会话
private Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//具体参数自己定
5. Destination
private Destination destination = session.createQueue(“text-msg”);//接收对应队列名
6. 消费者(消息接收者)
private MessageConsumer consumer = session.createConsumer(destination);
7. 接收消息
TextMessage message= (TextMessage) consumer.receive(100000);
String text = ((TextMessage)message).getText();
结语:
在http://127.0.0.1:8161/admin/中可以看到消息的发送接收情况,用户名密码都是admin
注:
如果结合spring使用,最好使用5.12.0之前的版本,之后的版本包含了spring会引起冲突而报错,或者剔除掉spring的内容再引入,不然tomcat都启动不了哦
1.创建连接
private String userName = “”;
private String password = “”;
private String brokerURL = “tcp://127.0.0.1:61616”;
2. connection的工厂
private ConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, brokerURL);
3. 连接对象
private Connection connection = factory.createConnection();
connection.start();
4. 会话
private Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//具体参数自己定
5. Destination
private Destination destination = session.createQueue(“text-msg”);//创建名为text-msg的队列
6. 生产者(消息发送者
private MessageProducer producer = session.createProducer(destination);
7. 发送消息
TextMessage textMsg = session.createTextMessage(“发送消息”);
producer.send(textMsg);
二. 消费者(消息接收者)
1.创建连接
private String userName = “”;
private String password = “”;
private String brokerURL = “tcp://127.0.0.1:61616”;
2. connection的工厂
private ConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, brokerURL);
3. 连接对象
private Connection connection = factory.createConnection();
connection.start();
4. 会话
private Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//具体参数自己定
5. Destination
private Destination destination = session.createQueue(“text-msg”);//接收对应队列名
6. 消费者(消息接收者)
private MessageConsumer consumer = session.createConsumer(destination);
7. 接收消息
TextMessage message= (TextMessage) consumer.receive(100000);
String text = ((TextMessage)message).getText();
结语:
在http://127.0.0.1:8161/admin/中可以看到消息的发送接收情况,用户名密码都是admin
注:
如果结合spring使用,最好使用5.12.0之前的版本,之后的版本包含了spring会引起冲突而报错,或者剔除掉spring的内容再引入,不然tomcat都启动不了哦
相关文章推荐
- 简单使用ActiveMQ
- ActiveMQ简单使用介绍
- Activemq的简单使用
- ActiveMQ的在实际项目中的简单使用
- ActiveMQ的在实际项目中的简单使用
- ActiveMQ安装和简单使用
- ActiveMq简单使用
- ubuntu下简单使用activemq
- Windows平台下的ActiveMQ消息队列的简单使用
- activeMQ搭建和简单使用
- spring整合消息中间件ActiveMQ简单使用
- 遇见JMS[1] —— activeMQ的简单使用
- ActiveMq简单使用
- JMS简介和ActiveMQ简单使用
- java消息队列ActiveMQ的简单使用
- 简单的activemq的封装和使用
- ActiveMQ学习(三)Java中使用AMQ的简单实例
- java消息队列ActiveMQ的简单使用
- ActiveMQ的简单使用
- ActiveMQ的简单使用