消息队列MQ

activeMQ
JMS(Java Messaging Service)是Java平台上有关面向消息中间件的技术规范,它便于消息系统中的Java应用程序进行消息交换,并且通过提供标准的产生、发送、接收消息的接口简化企业应用的开发。
JMS 定义了五种不同的消息正文格式,以及调用的消息类型,允许你发送并接收以一
些不同形式的数据,提供现有消息格式的一些级别的兼容性。
· TextMessage–一个字符串对象
· MapMessage–一套名称-值对
· ObjectMessage–一个序列化的 Java 对象
· BytesMessage–一个字节的数据流
· StreamMessage – Java 原始值的数据流

JMS消息传递类型

  • 点对点(queue):即一个生产者和一个消费者一一对应,消费后消息被删除
  • 发布/订阅(topic):即一个生产者产生消息并进行发送后,可以由多个消费者进
    行接收,接收后消息不会被删除。

activeMQ安装

安装包在虚拟机解压(即安装),然后启动,默认端口8161,用户名密码都是admin

入门案例

依赖

	<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-client</artifactId>
		<version>5.13.4</version>
	 </dependency>

生产者

//        创建连接工厂 tcp://ip:61616 
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://ip:61616");

    //        创建连接
            Connection connection = connectionFactory.createConnection();
    //        开启连接
            connection.start();

    //      获得session  boolean 是否开启事务
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    //      创建消息队列  test队列名称
        Destination destination = session.createTopic("test");

        //      创建生产者
        MessageProducer producer = session.createProducer(destination);

        //        创建消息
        TextMessage message = session.createTextMessage("ssssssss");
        //        发布消息
        producer.send(message);
        System.out.println("success");
    //        关闭资源
            producer.close();
            session.close();
            connection.close();

消费者
必须实现接口MessageListener并重写onMessage方法

public class ConsumerDemo implements MessageListener{

    public void onMessage(Message message) {
        TextMessage textMessage= (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

与spring整合
生产者
配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context   
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jms
		http://www.springframework.org/schema/jms/spring-jms.xsd">
	<!--组件扫描-->
	<context:component-scan base-package="cn.itcast.demo"></context:component-scan>        
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
	    <property name="brokerURL" value="tcp://ip:61616"/>
	</bean>          
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
	<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
	    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
	</bean>  
	


    <!-- Spring提供的JMS工具类,它可以进行消息发送消息 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
	    <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
	    <property name="connectionFactory" ref="connectionFactory"/>  
	</bean>      
    <!--这个是队列目的地,点对点的  文本信息 是Destination的子类-->
	<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<!--对列名-->
	    <constructor-arg value="queue_text"/>  
	</bean>   
	


</beans>

代码

@Component
public class ProducerDemo {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Destination activeMQQueue;

    public void sendMessage(){
        jmsTemplate.send(activeMQQueue, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("和spring整合的消息");
            }
        });
        System.out.println("ok");
    }

}

消费者
配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context   
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jms
		http://www.springframework.org/schema/jms/spring-jms.xsd">

    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
	    <property name="brokerURL" value="tcp://ip:61616"/>
	</bean>          
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
	<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
	    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
	</bean>  

    <!--这个是队列目的地,点对点的  文本信息-->  
	<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
	    <constructor-arg value="queue_text"/>  
	</bean>   

	<!-- 我的监听类 -->
	<bean id="myMessageListener" class="cn.itcast.demo.queue.ConsumerDemo"></bean>
	<!-- 消息监听容器 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<!--连接工厂-->
		<property name="connectionFactory" ref="connectionFactory" />
		<!--队列-->
		<property name="destination" ref="activeMQQueue" />
		<!--监听类-->
		<property name="messageListener" ref="myMessageListener" />
	</bean>	

</beans>

消费者(监听类)

@Component
public class ConsumerDemo implements MessageListener{

    public void onMessage(Message message) {
        TextMessage textMessage= (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

注意:要是用发布订阅模式(topic),必须先启动消费者,因为topic不处理旧的消息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值