博客
关于我
Spirng集成ActiveMQ
阅读量:548 次
发布时间:2019-03-09

本文共 7952 字,大约阅读时间需要 26 分钟。

使用Spirng集成ActiveMQ:

1.ConnectionFactory:用于管理连接的连接工厂

2.JmsTemplate:用于发送和接受消息的模板类

3.MessageListener:消息监听器

ConnectionFactory:

1.一个Spring提供的连接池

2.JmsTemplate每次发消息都会重新创建连接,会话和productor

3.Spring中提供了SingleConnectionFactory和CachingConnectionFactory

JmsTemplate:

1.由Spring提供,只需要向Spring容器注册这个类就能使用JmsTemplate方便的操作jms

MessageListerner:

1.实现一个onMessage方法,该方法只接受一个Message参数。

队列模式

1.首先导入jar包,这里我用的是maven

org.apache.activemq
activemq-all
5.7.0
org.apache.activemq
activemq-core
5.7.0
org.springframework
spring-jms
4.2.5.RELEASE
org.apache.activemq
activemq-pool
5.7.0
org.springframework
spring-context
4.2.5.RELEASE
org.springframework
spring-test
4.2.5.RELEASE
test
org.springframework
spring-test
5.0.6.RELEASE
compile

Spring与AMQ的配置文件:

1.因为有重合部分所以单独做了一个Commen配置:(spring-amq-commen.xml)

tcp://192.168.1.6:61616

2.发送者:(spring-amq-producer.xml)

3.接受者:(spring-amq-consumer.xml)

因为有用到JmsTemplate所以先定义一个接口:

package com.zzf.spring.jms.service;public interface ProducerServic {    public void sendMsg(String message);}

实现类:

package com.zzf.spring.jms.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service;import javax.annotation.Resource;import javax.jms.*;@Servicepublic class ProducerServiceImpl implements ProducerServic {    @Autowired    JmsTemplate jmsTemplate;    @Resource(name = "destinationQueue")    Destination destination;    public void sendMsg(final String message) {        //使用JmsTemplate发送消息        jmsTemplate.send(destination, new MessageCreator() {          //创建一个消息            public Message createMessage(Session session) throws JMSException {                TextMessage textMessage = session.createTextMessage(message);                return textMessage;            }        });        System.out.println("发送消息"+message);    }}

启动类(生产者):

package com.zzf.spring.jms.service;import org.springframework.context.support.ClassPathXmlApplicationContext;class AppProducer {    public static void main(String[] args) {        //读取配置文件        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-amq-producer.xml");        //ProducerServic producerServic= context.getBean(ProducerServic.class);//通过获取class的方式获取bean        ProducerServic producerServic= (ProducerServic) context.getBean("producerServiceImpl");        for (int i = 0; i < 10; i++) {            producerServic.sendMsg("Spring-test="+i);        }        context.close();    }}

启动后可以看到:

消费者需要实现MessageListerner做消息监听器:

package com.zzf.spring.jms.service;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class MyMessageListener implements MessageListener {    public void onMessage(Message msg) {        if (msg instanceof TextMessage) {            try {                TextMessage txtMsg = (TextMessage) msg;                String message = txtMsg.getText();                System.out.println("消息内容: " + message);            } catch (JMSException e) {                throw new RuntimeException(e);            }        } else {            throw new IllegalArgumentException("Message must be of type TextMessage");        }    }}

启动类:

package com.zzf.spring.jms.service;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppConsumer {    public static void main(String[] args) {        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-amq-consumer.xml");    }}
启动后:

OK……Spring与AMQ的队列模式就这么能用了

主题模式

1.修改xml文件添加主题模式:commen.xml

tcp://192.168.1.6:61616

修改配置容器,增加一个topic.xml

原来的consumer.xml如下所示:

修改接口:

package com.zzf.spring.jms.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service;import javax.annotation.Resource;import javax.jms.*;@Servicepublic class ProducerServiceImpl implements ProducerServic {    @Autowired    JmsTemplate jmsTemplate;    @Resource(name = "destinationQueue")    Destination destination;    @Resource(name = "destinationTopic")    Destination destinationTopic;    public void sendMsg(final String message) {        //使用JmsTemplate发送消息        jmsTemplate.send(destination, new MessageCreator() {            //创建一个消息            public Message createMessage(Session session) throws JMSException {                TextMessage textMessage = session.createTextMessage(message);                return textMessage;            }        });        System.out.println("发送消息"+message);    }    public void sendMsgTopic(final String message) {        //使用JmsTemplate发送消息        jmsTemplate.send(destinationTopic, new MessageCreator() {            //创建一个消息            public Message createMessage(Session session) throws JMSException {                TextMessage textMessage = session.createTextMessage(message);                return textMessage;            }        });        System.out.println("发送消息"+message);    }}

只需要修改Resource的name也行

发布者启动类代码:

package com.zzf.spring.jms.service;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppTopicProducer {    public static void main(String[] args) {        //读取配置文件        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-amq-producer.xml");        //ProducerServic producerServic= context.getBean(ProducerServic.class);//通过获取class的方式获取bean        ProducerServic producerServic= (ProducerServic) context.getBean("producerServiceImpl");        for (int i = 0; i < 10; i++) {            producerServic.sendMsgTopic("Spring-test="+i);        }        context.close();    }}
订阅者启动类代码:
package com.zzf.spring.jms.service;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AppTopicConsumer {    public static void main(String[] args) {        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-amq-topic.xml");    }}

效果显示:

每个订阅者都收到了发布者发送的消息~

你可能感兴趣的文章
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>