How to analyze the process of SpringBoot Integration ActiveMQ
How to analyze the SpringBoot integration ActiveMQ process, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Directory structure
Introduce maven dependency
Org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-activemq org.springframework.boot spring-boot-maven-plugin
Introduction of application.yml configuration
Spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: adminqueue: springboot-queueserver: port: 8080
Create QueueConfig
@ Configurationpublic class QueueConfig {@ Value ("${queue}") private String queue; @ Bean public Queue logQueue () {return new ActiveMQQueue (queue);} @ Bean public JmsTemplate jmsTemplate (ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {JmsTemplate jmsTemplate = new JmsTemplate (); jmsTemplate.setDeliveryMode (2); / / persistent configuration 1 means non-persistent, 2 means persistent jmsTemplate.setConnectionFactory (activeMQConnectionFactory); jmsTemplate.setDefaultDestination (queue) / / No default can be set here, and queue jmsTemplate.setSessionAcknowledgeMode (4) can also be set when sending messages; / / client sign-off mode return jmsTemplate;} / / defines a message listener connection factory, which defines a point-to-point listener connection factory @ Bean (name = "jmsQueueListener") public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory (ActiveMQConnectionFactory activeMQConnectionFactory) {DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory (); factory.setConnectionFactory (activeMQConnectionFactory) / / set the number of connections factory.setConcurrency ("1-10"); / / reconnect interval factory.setRecoveryInterval (1000L); factory.setSessionAcknowledgeMode (4); return factory;}}
Create a producer:
@ SpringBootApplication@Component@EnableSchedulingpublic class Producer {@ Autowired private JmsMessagingTemplate jmsMessagingTemplate; @ Autowired private Queue queue; @ Scheduled (fixedDelay=3000) public void send () {String result = System.currentTimeMillis () + "--Test"; System.out.println ("result" + result); jmsMessagingTemplate.convertAndSend (queue,result);} public static void main (String [] args) {SpringApplication.run (Producer.class, args);}}
Create a consumer's application.yml
Spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: adminqueue: springboot-queueserver: port: 8081
To create a consumer:
@ Component@SpringBootApplicationpublic class consumer {private int count = 0; @ JmsListener (destination = "${queue}") public void receive (TextMessage textMessage,Session session) throws JMSException {String text = textMessage.getText (); System.out.println ("consumption:" + text+ "get message count:" + (+ + count)); System.out.println (); String jmsMessageID = textMessage.getJMSMessageID ();} public static void main (String [] args) {SpringApplication.run (consumer.class,args);}}
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.