ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《一》
来源:互联网 发布:华夏网络危机公关公司 编辑:程序博客网 时间:2024/06/14 17:04
新手刚刚学习ActiveMQ,从网上找了很多案例,查阅请教之后总结,接收端监听有些小问题,使用线程来接收,希望可以多多评论互相学习。如有相同请联系删除。
项目A发送消息端:
// TODO Auto-generated method stubConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS// Provider 的连接Connection connection = null; // Session: 一个发送或接收消息的线程Session session; // Destination :消息的目的地;消息发送给谁.Destination destination; // MessageProducer:消息发送者MessageProducer producer; // TextMessage message;// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jarconnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");try { // 构造从工厂得到连接对象connection = connectionFactory.createConnection();// 启动connection.start();// 获取操作连接session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置destination = session.createQueue("FirstQueue");// 得到消息生成者【发送者】producer = session.createProducer(destination);// 设置不持久化,此处学习,实际根据项目决定producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 构造消息,此处写死,项目就是参数,或者方法获取ActiveMQObjectMessage msg = (ActiveMQObjectMessage) session.createObjectMessage();
msg.setObject((Serializable) user);//将对象发送producer.send(msg);session.commit();} catch (Exception e) {e.printStackTrace();} finally {try {if (null != connection)connection.close();} catch (Throwable ignore) {}}项目B 接受对象:
因为使用监听一直触发不了,没有解决,所以使用项目运行时开启线程持续接收消息。
开启线程:
public class MyListener implements ServletContextListener {private MyThread myThread; public void contextDestroyed(ServletContextEvent e) { if (myThread != null && myThread.isInterrupted()) { myThread.interrupt(); } } public void contextInitialized(ServletContextEvent e) { String str = null; if (str == null && myThread == null) { myThread = new MyThread(); myThread.start(); // servlet 上下文初始化时启动 socket } } }
线程开启接收消息:
public class MyThread extends Thread {public void run() {while (!this.isInterrupted()) {// 线程未中断执行循环try {Thread.sleep(200); // 每隔2000ms执行一次} catch (InterruptedException e) {e.printStackTrace();}// ------------------ 开始执行 ---------------------------// ConnectionFactory :连接工厂,JMS 用它创建连接ConnectionFactory connectionFactory;// Connection :JMS 客户端到JMS Provider 的连接Connection connection = null;// Session: 一个发送或接收消息的线程Session session;// Destination :消息的目的地;消息发送给谁.Destination destination;// 消费者,消息接收者MessageConsumer consumer;connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");try {// 构造从工厂得到连接对象connection = connectionFactory.createConnection();// 启动connection.start();// 获取操作连接session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置destination = session.createQueue("FirstQueue");consumer = session.createConsumer(destination);consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {// TODO Auto-generated method stubUser user;try {user = (User) ((ObjectMessage) message).getObject();if (message != null) {// User s = (User) message.getObject();System.out.println("收到的消息对象:"+ user.getLoginName());user.setCreateBy(new User("1"));user.setUpdateBy(new User("1"));//使用getBean方式获取Bean 因注解方式扫描不到.SystemService systemService = (SystemService)ApplicationContextHandle.getBean("systemService");systemService.saveUser(user);}} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});} catch (Exception e) {e.printStackTrace();} finally {try {if (null != connection)connection.close();} catch (Throwable ignore) {}}}}}
配置Web.xml 配置线程监听。项目开启时启动线程<listener> <listener-class>com.thinkgem.jeesite.modules.test.web.MyListener</listener-class></listener>
阅读全文
1 0
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《一》
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
- ActiveMq发送接收对象
- activemq发送和接收对象
- activemq 对象的传输
- C# 发送接收序列化对象
- SpringMVC 接收ajax发送的数组对象
- BW项目的对象传输
- jmeter(postman)发送请求对象,springmvc接收对象
- asp.net 中Ajax发送XML对象到服务器和服务器获取XML对象
- 往服务器发送自定义的对象
- flex socket.writeObject()发送对象,java接收flex socket发送过来的对象
- spring整合ActiveMQ订阅模式(对象消息发送)
- jersey接收对象参数到服务器
- 发送接收的项目总结
- DWR传输和接收对象
- 10XMLHttpRequest对象的创建,及发送和接收数据
- springMVC如何接收和发送json数据对象
- Android Volley的使用(三)Volley中如何使用https
- Pytorch学习笔记(一):pytorch的安装-Ubuntu14.04
- Sort排序====javascript函数另一种写法
- 6. 用冒泡法实现对10个整数按从小到大的顺序排序输出
- 体系架构,TCP/UDP,http
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《一》
- 数组总结
- 数据结构心得总结(持久更新)
- HDU 1173 采矿
- jquery post请求参数和接收方式
- 【精通hibernate】(3)hibernate api 操作Java对象
- 虚析构函数,纯虚析构函数~~~
- VS编译错误C4996'scanf': This function or variable may be unsafe.
- minicom使用时的问题