//创建连接工厂
ConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");
Connection conn=null;
Queue queue=new ActiveMQQueue("testQueue");
try { //创建连接
conn=factory.createConnection();
conn.start();
//创建Session
Session session=conn.createSession(false,
Session.AUTO_ACKNOWLEDGE);
//创建Message
Message message=session.createTextMessage("Hello World!");
//创建消息生产者
MessageProducer producer=session.createProducer(queue);
//发送消息
producer.send(message);
//创建消息接收
MessageConsumer consumer=session.createConsumer(queue);
Message receiver= consumer.receive();
System.out.println(((TextMessage)receiver).getText());
} catch (JMSException e) {
e.printStackTrace();
}
JMS实例
最新推荐文章于 2022-10-28 21:05:22 发布