Two modes: queue mode / topic mode
pom.xml
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
In fact, the queuing mode is the food sharing mode.
For example, if the manufacturer sends 10 messages to the activeMQ server, and there are multiple consumers at this time, these consumers will divide these 10 messages, and only one consumer will get one message.
Theme mode is subscription mode.
For example, the producer has sent 10 messages, and at this time there are multiple consumers, so many consumers can get these 10 messages, just like subscribing to the public number.
Queue mode:
1. Run the TestConsumer class twice to start two different consumers
2. Run TestProducer once to start the producer
Producers produce 100, and two consumers divide up
Consumer:
public class TestConsumer { //Service address, port default 61616 private static final String url="tcp://127.0.0.1:61616"; //Message name of this consumption private static final String topicName="queue_style"; //There may be multiple consumers. In order to distinguish different consumers, create random names for them private static final String consumerName="consumer-" + RandomUtil.randomString(5); public static void main(String[] args) throws JMSException { //0. First, judge whether the port is started Active MQ The server ActiveMQUtil.checkServer(); System.out.printf("%s Consumer started. %n", consumerName); //1.Establish ConnectiongFactory,Binding address ConnectionFactory factory=new ActiveMQConnectionFactory(url); //2.Establish Connection Connection connection= factory.createConnection(); //3.Start connection connection.start(); //4.Create session Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5.Create a target (topic type) Destination destination=session.createQueue(topicName); //6.Create a consumer MessageConsumer consumer=session.createConsumer(destination); //7.Create a listener consumer.setMessageListener(new MessageListener() { public void onMessage(Message arg0) { // TODO Auto-generated method stub TextMessage textMessage=(TextMessage)arg0; try { System.out.println(consumerName +" Receive message:"+textMessage.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); //8. I don't know when it will be, so I can't shut it down. I'm not shutting it down. I've been monitoring it //connection.close(); } }
Producer:
public class TestProducer { //Service address, port default 61616 private static final String url="tcp://127.0.0.1:61616"; //The name of the message sent this time private static final String topicName="queue_style"; public static void main(String[] args) throws JMSException { //0. First, judge whether the port is started Active MQ The server ActiveMQUtil.checkServer(); //1.Establish ConnectiongFactory,Binding address ConnectionFactory factory=new ActiveMQConnectionFactory(url); //2.Establish Connection Connection connection= factory.createConnection(); //3.Start connection connection.start(); //4.Create session Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5.Create a goal (Queue type) Destination destination=session.createQueue(topicName); //6.Create a producer MessageProducer producer=session.createProducer(destination); for (int i = 0; i < 100; i++) { //7.Create message TextMessage textMessage=session.createTextMessage("Queue message-"+i); //8.send message producer.send(textMessage); System.out.println("Send out:"+textMessage.getText()); } //7. Close connection connection.close(); } }
2 consumer s:
Producer production:
Theme mode:
Consumer, producer
public class TestConsumer { //Service address, port default 61616 private static final String url="tcp://127.0.0.1:61616"; //Message name of this consumption private static final String topicName="topic_style"; //There may be multiple consumers. In order to distinguish different consumers, create random names for them private static final String consumerName="consumer-" + RandomUtil.randomString(5); public static void main(String[] args) throws JMSException { //0. First, judge whether the port is started Active MQ The server ActiveMQUtil.checkServer(); System.out.printf("%s Consumer started. %n", consumerName); //1.Establish ConnectiongFactory,Binding address ConnectionFactory factory=new ActiveMQConnectionFactory(url); //2.Establish Connection Connection connection= factory.createConnection(); //3.Start connection connection.start(); //4.Create session Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5.Create a target (topic type) Destination destination=session.createTopic(topicName); //6.Create a consumer MessageConsumer consumer=session.createConsumer(destination); //7.Create a listener consumer.setMessageListener(new MessageListener() { public void onMessage(Message arg0) { // TODO Auto-generated method stub TextMessage textMessage=(TextMessage)arg0; try { System.out.println(consumerName +" Receive message:"+textMessage.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); //8. I don't know when it will be, so I can't shut it down. I'm not shutting it down. I've been monitoring it //connection.close(); } }
public class TestProducer { //Service address, port default 61616 private static final String url="tcp://127.0.0.1:61616"; //The name of the message sent this time private static final String topicName="topic_style"; public static void main(String[] args) throws JMSException { //0. First, judge whether the port is started Active MQ The server ActiveMQUtil.checkServer(); //1.Establish ConnectiongFactory,Binding address ConnectionFactory factory=new ActiveMQConnectionFactory(url); //2.Establish Connection Connection connection= factory.createConnection(); //3.Start connection connection.start(); //4.Create session Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5.Create a goal (Topic type) Destination destination=session.createTopic(topicName); //6.Create a producer MessageProducer producer=session.createProducer(destination); for (int i = 0; i < 100; i++) { //7.Create message TextMessage textMessage=session.createTextMessage("Topic message-"+i); //8.send message producer.send(textMessage); System.out.println("Send out:"+textMessage.getText()); } //7. Close connection connection.close(); } }
Producers produce 100, and both consumers accept 100