In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to integrate Activemq in Springboot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1 Import the dependencies required for integration:
Org.springframework.boot spring-boot-starter-activemq
2 create an application.properties file
Spring.activemq.broker-url=tcp://127.0.0.1:61616spring.activemq.user=adminspring.activemq.password=adminserver.port=8080queue=myqueue
3. Custom profile QueueConfig reads the queue name of the configuration file and creates a Queue based on the queue name
Package com.example.demo;import javax.jms.Queue;import org.apache.activemq.ActiveMQConnectionFactory;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.config.DefaultJmsListenerContainerFactory;import org.springframework.jms.core.JmsTemplate;@Configurationpublic class QueueConfig {@ Value ("${queue}") private String queue; @ Bean public Queue logQueue () {return new ActiveMQQueue (queue) }}
4. To create a producer, you can directly use the provided template JmsMessagingTemplate to send messages:
Package com.example.demo.producter;import javax.jms.Queue;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Component;import com.example.demo.SpringbootActivemqApplication;@Componentpublic class Producter {@ Autowired private JmsMessagingTemplate jmsMessagingTemplate; @ Autowired private Queue queue; private static Logger logger = LoggerFactory.getLogger (Producter .class) Public void send () {String str = "producer production data:" + System.currentTimeMillis (); jmsMessagingTemplate.convertAndSend (queue, str); logger.info ("producer data: {}", str);}}
5. Startup class:
Package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.scheduling.annotation.EnableScheduling;import com.example.demo.producter.Producter;import com.example.demo.producter.consumer.Consumer;@SpringBootApplication@EnableSchedulingpublic class SpringbootActivemqApplication implements ApplicationListener {@ Autowired public Producter producter; @ Autowired public Consumer consumer Public static void main (String [] args) {SpringApplication.run (SpringbootActivemqApplication.class, args); / / onApplicationEvent method runs this method when starting springboot. You can choose the appropriate method to call the message according to the actual situation of the project} @ Override public void onApplicationEvent (ContextRefreshedEvent event) {producter.send ();}}
6. Start the project and output from the console:
7. It is easy to create consumers. You only need to listen to the queue:
Package com.example.demo.producter.consumer;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic class Consumer {@ JmsListener (destination = "${queue}") public void receive (String msg) {System.out.println ("listener receives msg:" + msg);}}
8. The final result:
This is the end of the content of "how to integrate Activemq in Springboot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.