Basic Queue message sending and consumption

Configure the dependencies required by Maven, an example is as follows

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.2.0</version>
</dependency>

<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>3.7</version>
</dependency>

The sample code for sending Queue messages is as follows:

public class JmsProducer {

    public static void main(String[] args) {
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://39.96.192.171:61616");
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("my-queue");
            MessageProducer producer = session.createProducer(destination);
            for (int i = 0; i < 3; i++) {
                TextMessage message = session.createTextMessage("message--" + i);

                Thread.sleep(1000);
                //通过消息生产者发出消息
                producer.send(message);
            }
            session.commit();
            session.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Then run this class

You can see that in our custom my-queue in the queue, there are 3 messages that have not been consumed!

Next we write consumers to consume messages!

public class JmsReceiver {
    public static void main(String[] args) {
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://39.96.192.171:61616");
            Connection connection = cf.createConnection();
            connection.start();
            final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("my-queue");
            MessageConsumer consumer = session.createConsumer(destination);
            int i = 0;
            while (i < 3) {
                i++;
                TextMessage message = (TextMessage) consumer.receive();
                session.commit();
                System.out.println("收到消息:" + message.getText());
            }
            session.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Run this class! You can see that the console has received 3 messages!

Look at the activemq console again

You can see that there are 3 messages that are consumed!

This is simple message production and consumption! Message consumption is divided into two ways, synchronous consumption and asynchronous consumption. The above example is synchronous consumption. By calling the consumer's receive method to explicitly extract the message from the destination, the receive method can block until the message arrives.

Then write an example of asynchronous consumption!

public class JmsReceiver2 implements MessageListener {

    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            if (null != message) {
                System.out.println("收到的消息:" + textMessage.getText());
            }
            // 如果session设置为Session.CLIENT_ACKNOWLEDGE,要加上这一步
            message.acknowledge();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41023026/article/details/89892248
Recommended