JMS - ActiveMQ的简单使用
2014-11-03 18:36
447 查看
首先需要下载ActiveMQ,下面的链接给我们列出了所有版本: http://activemq.apache.org/download-archives.html 每个版本为不同的OS提供了链接:
公司电脑是windows的,用目录下的activemq.bat启动:
端口号默认是61616,可以在conf/activemq.xml中看到:
相关的Maven dependency:
使用javax.jms.Session跟JMS Provider通信:
然后一些目的地、发送者、发送内容什么的都是由session来弄的:
有了producer,相应地也有consumer,接收消息方法如下:
结果还是consumer去一个个receive了,就像是接收人亲自去确认那样。
或许我们可以让Listener代劳:
当这个consumer设置了Listener的时候就不能再以receive()的方式接收了,
不然会出现javax.jms.IllegalStateException:Cannot synchronously receive a message when a MessageListener is set
如果想使用publish/subscribe,直接将createQueue改为createTopic即可,但需要理解Topic是无状态的。
完整code如下,发送者:
接收者:
公司电脑是windows的,用目录下的activemq.bat启动:
端口号默认是61616,可以在conf/activemq.xml中看到:
<transportConnectors> <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB --> <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/> </transportConnectors>
相关的Maven dependency:
<dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency>
使用javax.jms.Session跟JMS Provider通信:
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
然后一些目的地、发送者、发送内容什么的都是由session来弄的:
Destination destination = session.createQueue("this is sparta!!"); MessageProducer producer = session.createProducer(destination); TextMessage message0 = session.createTextMessage("这是斯巴达!!!"); TextMessage message1 = session.createTextMessage("这也是斯巴达!!!"); TextMessage message2 = session.createTextMessage("这些都是斯巴达!!!"); producer.send(message0); producer.send(message1); producer.send(message2); session.commit();
有了producer,相应地也有consumer,接收消息方法如下:
MessageConsumer consumer = session.createConsumer(destination); System.out.println(((TextMessage) consumer.receive(10000)).getText());
结果还是consumer去一个个receive了,就像是接收人亲自去确认那样。
或许我们可以让Listener代劳:
consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { try { System.out.println("listener catched:::"+((TextMessage)message).getText()); } catch (JMSException e) { e.printStackTrace(); } } });
当这个consumer设置了Listener的时候就不能再以receive()的方式接收了,
不然会出现javax.jms.IllegalStateException:Cannot synchronously receive a message when a MessageListener is set
如果想使用publish/subscribe,直接将createQueue改为createTopic即可,但需要理解Topic是无状态的。
完整code如下,发送者:
{ ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("this is sparta!!"); MessageProducer producer = session.createProducer(destination); TextMessage message0 = session.createTextMessage("这是斯巴达!!!"); TextMessage message1 = session.createTextMessage("这也是斯巴达!!!"); TextMessage message2 = session.createTextMessage("这些都是斯巴达!!!"); producer.send(message0); producer.send(message1); producer.send(message2); session.commit(); }
接收者:
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");;
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("this is sparta!!");
MessageConsumer consumer = session.createConsumer(destination); System.out.println(((TextMessage) consumer.receive(10000)).getText());
System.out.println(((TextMessage) consumer.receive(10000)).getText());
System.out.println(((TextMessage) consumer.receive(10000)).getText());
}
相关文章推荐
- ActiveMq-JMS简单实例使用tomcat
- JMS简介和ActiveMQ简单使用
- 一个简单的JMS实例(使用ActiveMQ)
- 遇见JMS[1] —— activeMQ的简单使用
- spring-jms(activemq实现)使用queue发送消息简单例子
- 【Java.JMS】ActiveMQ的基本使用
- ACTIVEMQ实现JMS的简单实例
- 使用activeMQ实现jms
- 使用apache的activemq集合JMS处理异步消息
- 简单jms连接池和一点使用心得
- 简单的activemq的封装和使用
- Apache apusic---ActiveMQ中间件在JMS1.1、J2EE中的使用
- JMS Apache ActiveMQ(消息中间件)使用攻略
- 使用activeMQ实现jms
- activeMQ,spring的jmstemplate简单例子
- Spring+JMS+使用JMS+ActiveMQ中间件+实现简单聊天功能的小例子
- JMS_使用ActiveMQ实现消息的发送和接收
- activeMQ,spring的jmstemplate简单例子
- activeMQ,spring的jmstemplate简单例子
- spring 使用 ActiveMQ,JMS使用