1、启动ActiveMQ服务器
2、消息的发送
import java.util.Date; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * 消息的发送者 */ public class Producer { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; String url = ActiveMQConnection.DEFAULT_BROKER_URL; String subject = "TOOL.DEFAULT"; /**创建连接工厂*/ ActiveMQConnectionFactory contectionFactory = new ActiveMQConnectionFactory( user, password, url); try { /**创建连接*/ Connection connection = contectionFactory.createConnection(); connection.start();//开启连接 /**创建session*/ Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); /**指定消息的目的地*/ Destination destination = session.createQueue(subject); /**创建发送消息对象*/ MessageProducer producer = session.createProducer(destination); for (int i = 0; i <= 2; i++) { MapMessage message = session.createMapMessage(); Date date = new Date(); message.setLong("date", date.getTime()); message.setString("seqnum", String.valueOf(i)); Thread.sleep(1000); producer.send(message); System.out.println("发送端发送消息:" + date); } session.commit(); session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
3、消息的消费
/** * queue:消息消费者 * * @author 文超 * @version [版本号, Apr 27, 2013 5:28:49 PM ] * @see [相关类/方法] * @since [产品/模块版本] */ public class Consumer { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; String url = ActiveMQConnection.DEFAULT_BROKER_URL; String subject = "TOOL.DEFAULT"; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); Connection connection; try { connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(subject); MessageConsumer message = session.createConsumer(destination); message.setMessageListener(new MessageListener() { public void onMessage(Message msg) { MapMessage message = (MapMessage) msg; try { System.out.println("接收端收到消息," +message.getString("seqnum")+":"+ new Date(message.getLong("data"))); session.commit(); } catch (JMSException e) { e.printStackTrace(); } } }); Thread.sleep(3000); session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
相关推荐
activeMQ之hello world小程序
ActiveMQ实现例子:借助中间件ActiveMQ实现消息传输。
activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载
activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子activeMq的一个小例子...
activemq实战
#ActiveMQ DEMO 管理页面: activemq 端口:61616 activemq start:apache-activemq-5.11.1/bin/macosx/activemq start
ActiveMQ_使用failover模式进行连接切换时,线程断开 ,ActiveMQ_使用failover模式进行连接切换时,线程断开
JMS、ActiveMQ入门及深入使用的例子,这些例子都是我测试的
我们使用ActiveMQ为大家实现一种点对点的消息模型。如果你对点对点模型的认识较浅,可以看一下第一篇博文的介绍。
ActiveMQ例子
activemq-cpp:Apache ActiveMQ CPP
ActiveMQ允许客户端使用多种协议接收消息,其中WebSocket协议的URL使用ws://开头,默认端口号是61614
标签:activemq-web-4.0-M3.jar.zip,activemq,web,4.0,M3,jar.zip包下载,依赖包
activemq-activeio:Apache ActiveMQ ActiveIO的镜像
activemq-protobuf:Apache ActiveMQ Protobuf的镜像
activeMQ与spring整合开发的例子程序,消息队列技术
springMVC整合activeMQ的例子
赠送jar包:activemq-core-5.7.0.jar; 赠送原API文档:activemq-core-5.7.0-javadoc.jar; 赠送源代码:activemq-core-5.7.0-sources.jar; 包含翻译后的API文档:activemq-core-5.7.0-javadoc-API文档-中文...
activemq 虚拟topic配置,可以将一个 topic转发为多个队列和多个topic或者将一个队列转发为多个topic和多个队列
n 四:用ActiveMQ构建应用 包括:多种启动Broker的方法、单独应用的开发、结合Spring的开发等 n 五:ActiveMQ的Transport 包括:多种传输协议的功能、配置和使用 六: ActiveMQ的消息存储 包括:队列和topic、KahaDB...