In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Java SpringBoot how to integrate ActiveMQ, many novices are not very clear, 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.
1. If you want to use ActiveMQ components in your project, you should add dependency support libraries to the project and modify the pom.xml configuration file:
Org.springframework.boot spring-boot-starter-activemq
Second, modify the application.yml configuration file to configure activemq
Server: port: 80spring: messages: basename: i18n Queue messages jms: pub-sub-domain: false # configure the type of message, if it is true, it is topic message, if it is false, it means Queue message activemq: user: studyjava # connection user name password: hello # connection password broker-url: tcp://activemq-server:61616 # connection host information of the message component
3. Then define the consumer of a message. The consumer mainly carries out a monitoring control. In SpringBoot, you can directly use the annotation @ JmsListener to monitor:
Package cn.study.microboot.consumer;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Service;@Servicepublic class MessageConsumerService {@ JmsListener (destination= "study.msg.queue") public void receiveMessage (String text) {/ / perform message reception processing System.err.println ("[* receive message * *]" + text);}}
4. Then establish the sender service of the message. Generally speaking, if you send the message, you will often prepare a business interface:
Package cn.study.microboot.producer;public interface IMessageProducerService {public void sendMessage (String msg);}
5. Then set up a configuration program class and define the message sending template processing class of ActiveMQ:
Package cn.study.microboot.config;import javax.jms.Queue;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.annotation.EnableJms;@Configuration@EnableJmspublic class ActiveMQConfig {@ Bean public Queue queue () {return new ActiveMQQueue ("study.msg.queue");}}
6. Create a subclass of message sending to implement message sending processing:
Package cn.study.microboot.producer.impl;import javax.annotation.Resource;import javax.jms.Queue;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Service;import cn.study.microboot.producer.IMessageProducerService;@Servicepublic class MessageProducerServiceImpl implements IMessageProducerService {@ Resource private JmsMessagingTemplate jmsMessagingTemplate; @ Resource private Queue queue; @ Override public void sendMessage (String msg) {this.jmsMessagingTemplate.convertAndSend (this.queue, msg);}}
7. Write a test class to observe the processing of messages:
Package cn.study.microboot.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import cn.study.microboot.StartSpringBootMain;import cn.study.microboot.producer.IMessageProducerService;@SpringBootTest (classes = StartSpringBootMain.class) @ RunWith (SpringJUnit4ClassRunner.class) @ WebAppConfigurationpublic class TestActiveMQ {@ Resource private IMessageProducerService messageProducer @ Test public void testSend () throws Exception {for (int x = 0; x < 10; x +) {this.messageProducer.sendMessage ("study -" + x);} will it help 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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.