您的位置:首页 > 其它

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都启动不了哦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: