JMS消息发送接收举例

Posted zengzil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JMS消息发送接收举例相关的知识,希望对你有一定的参考价值。

1.需要的jar包

 

<dependency>

         <groupId>org.apache.activemq</groupId>

         <artifactId>activemq-all</artifactId>

         <version>5.15.4</version>

</dependency>

<dependency>

        <groupId>org.apache.xbean</groupId>

        <artifactId>xbean-spring</artifactId>

        <version>4.8</version>

</dependency>

 

2.消息创建和发送QueueProducer类

 

package com.jmstest;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

 

public class QueueProducer{

 

    public static void main(String[] args) throws JMSException {

 

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.145.129:61616");

        Connection connection = connectionFactory.createConnection();

        connection.start();

 

        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createQueue("ActiveMQ");

        MessageProducer producter = session.createProducer(destination);

 

        for (int i = 0; i < 3; i++) {

            TextMessage message = session.createTextMessage("ActiveMQ-->" + i);

 

            producter.send(message);

        }

 

        session.commit();

        session.close();

        connection.close();

    }

}

3.  消息消费接收QueueConsumer类

package com.jmstest;

import javax.jms.Connection;

import javax.jms.ConnectionFactory;

import javax.jms.Destination;

import javax.jms.MessageConsumer;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

 

public class QueueConsumer{

 

    public static void main(String[] args) throws Exception {

        //创建连接工厂并开启

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.145.129:61616");

        Connection connection = connectionFactory.createConnection();

        connection.start();

        final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createQueue("ActiveMQ");

        MessageConsumer consumer = session.createConsumer(destination);

       

        int i=0;

        while(i <3) {

            TextMessage message = (TextMessage) consumer.receive();

            session.commit();

            System.out.println("收到的消息:"+message.getText());

            i++;

        }

        session.close();

        connection.close();

    }

}

4.执行消费端程序

技术分享图片

 

5.可以从ActiveMQ控制台看到发送的消息

技术分享图片

 

 

 

以上是关于JMS消息发送接收举例的主要内容,如果未能解决你的问题,请参考以下文章

ActiveMQ的介绍及使用

Spring Boot的JMS发送和接收队列消息,基于ActiveMQ

Spring使用Spring和AMQP发送接收消息(上)

JMS学习

Spring使用MappingJackson2MessageConverter发送接收ActiveMQ消息

Spring整合JMS——MessageConverter介绍