java - Create durable topic and subscriber spring boot jms with ActiveMQ

Java - Create durable topic and subscriber spring boot jms with ActiveMQ

To create a durable topic and subscriber in a Spring Boot application using ActiveMQ, you'll need to configure both your application and your ActiveMQ broker appropriately. Here's a step-by-step guide:

1. Add Dependencies

First, add the necessary dependencies to your pom.xml file if you're using Maven:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> </dependency> </dependencies> 

2. Configure ActiveMQ

You can configure the connection to ActiveMQ in your application.properties file:

spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.packages.trust-all=true 

3. Configure JMS in Spring Boot

Create a configuration class to configure the JMS components:

import org.apache.activemq.ActiveMQConnectionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; @Configuration @EnableJms public class JmsConfig { @Bean public ActiveMQConnectionFactory connectionFactory() { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("tcp://localhost:61616"); connectionFactory.setUserName("admin"); connectionFactory.setPassword("admin"); return connectionFactory; } @Bean public JmsListenerContainerFactory<?> jmsListenerContainerFactory() { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory()); factory.setPubSubDomain(true); // Enable pub-sub (topics) mode return factory; } } 

4. Create a Durable Subscriber

Define a class that listens to messages from the topic:

import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class DurableSubscriber { @JmsListener(destination = "test-topic", containerFactory = "jmsListenerContainerFactory", subscription = "myDurableSubscription") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } 

5. Send a Message to the Topic

Create a service to send messages to the topic:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Service; @Service public class TopicSender { @Autowired private JmsTemplate jmsTemplate; public void sendMessage(String topic, String message) { jmsTemplate.convertAndSend(topic, message); } } 

6. Main Application Class

Ensure your main application class is annotated with @SpringBootApplication:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JmsApplication { public static void main(String[] args) { SpringApplication.run(JmsApplication.class, args); } } 

7. Testing the Setup

To test the setup, you can create a simple REST controller or a command-line runner to send a message to the topic:

REST Controller Example:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class JmsController { @Autowired private TopicSender topicSender; @GetMapping("/send") public String sendMessage(@RequestParam String message) { topicSender.sendMessage("test-topic", message); return "Message sent!"; } } 

CommandLineRunner Example:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class JmsCommandLineRunner implements CommandLineRunner { @Autowired private TopicSender topicSender; @Override public void run(String... args) throws Exception { topicSender.sendMessage("test-topic", "Hello, world!"); } } 

8. Run the Application

Start your Spring Boot application and send a message to the topic. If everything is configured correctly, the durable subscriber should receive the message and print it to the console.

Notes:

  • Ensure that your ActiveMQ broker is running on tcp://localhost:61616 or adjust the configuration accordingly.
  • If you encounter any issues, check the logs for detailed error messages and ensure all dependencies are correctly added.

This setup provides a basic yet functional example of creating a durable topic and subscriber with Spring Boot and ActiveMQ.

Examples

  1. How to create a durable topic and subscriber using Spring Boot JMS with ActiveMQ?

    Description: This query seeks guidance on creating a durable topic and subscriber configuration using Spring Boot JMS with ActiveMQ.

    @Configuration public class JmsConfig { @Bean public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); configurer.configure(factory, connectionFactory); factory.setPubSubDomain(true); // Set to true for topics factory.setSubscriptionDurable(true); // Set to true for durable subscription return factory; } } 

    Description: Configure a JmsListenerContainerFactory bean to create a durable subscriber. Set pubSubDomain to true for topics and subscriptionDurable to true for a durable subscription.

  2. Creating durable topic and subscriber Spring Boot JMS with ActiveMQ

    Description: This query specifically focuses on creating a durable topic and subscriber configuration using Spring Boot JMS with ActiveMQ.

    @Bean public ConnectionFactory connectionFactory() { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); factory.setBrokerURL("tcp://localhost:61616"); factory.setUserName("admin"); factory.setPassword("admin"); return factory; } 

    Description: Define a ConnectionFactory bean to establish a connection with the ActiveMQ broker, specifying the broker URL, username, and password.

  3. Spring Boot JMS - Durable topic and subscriber setup with ActiveMQ

    Description: This query seeks information on setting up a durable topic and subscriber using Spring Boot JMS with ActiveMQ.

    @JmsListener(destination = "myTopic", containerFactory = "jmsListenerContainerFactory") public void receiveMessage(String message) { // Message handling logic } 

    Description: Use the @JmsListener annotation to define a method for receiving messages from a topic. Specify the topic name and the JmsListenerContainerFactory bean to use.

  4. How to configure a durable subscriber for a topic in Spring Boot JMS with ActiveMQ?

    Description: This query focuses on configuring a durable subscriber for a topic using Spring Boot JMS with ActiveMQ.

    @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); configurer.configure(factory, connectionFactory); factory.setPubSubDomain(true); // Set to true for topics factory.setSubscriptionDurable(true); // Set to true for durable subscription return factory; } 

    Description: Define a DefaultJmsListenerContainerFactory bean to configure the durable subscriber. Set pubSubDomain to true for topics and subscriptionDurable to true for a durable subscription.

  5. Spring Boot JMS - Create durable topic and subscriber configuration

    Description: This query looks for guidance on creating a durable topic and subscriber configuration using Spring Boot JMS.

    @JmsListener(destination = "myTopic", containerFactory = "jmsListenerContainerFactory") public void receiveMessage(String message) { // Message handling logic } 

    Description: Use the @JmsListener annotation to define a method for receiving messages from a topic. Specify the topic name and the JmsListenerContainerFactory bean to use.

  6. Creating durable topic subscriber in Spring Boot JMS with ActiveMQ

    Description: This query specifically seeks guidance on creating a durable topic subscriber in Spring Boot JMS with ActiveMQ.

    @Bean public ActiveMQConnectionFactory connectionFactory() { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); factory.setBrokerURL("tcp://localhost:61616"); factory.setUserName("admin"); factory.setPassword("admin"); return factory; } 

    Description: Define an ActiveMQConnectionFactory bean to establish a connection with the ActiveMQ broker, specifying the broker URL, username, and password.

  7. Spring Boot JMS - Configuring durable topic subscriber with ActiveMQ

    Description: This query focuses on configuring a durable topic subscriber using Spring Boot JMS with ActiveMQ.

    @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); configurer.configure(factory, connectionFactory); factory.setPubSubDomain(true); // Set to true for topics factory.setSubscriptionDurable(true); // Set to true for durable subscription return factory; } 

    Description: Define a DefaultJmsListenerContainerFactory bean to configure the durable topic subscriber. Set pubSubDomain to true for topics and subscriptionDurable to true for a durable subscription.

  8. How to create a durable topic and subscriber in Spring Boot JMS with ActiveMQ programmatically?

    Description: This query seeks information on programmatically creating a durable topic and subscriber in Spring Boot JMS with ActiveMQ.

    @Configuration public class JmsConfig { @Bean public ActiveMQConnectionFactory connectionFactory() { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); factory.setBrokerURL("tcp://localhost:61616"); factory.setUserName("admin"); factory.setPassword("admin"); return factory; } } 

    Description: Define a ConnectionFactory bean programmatically to establish a connection with the ActiveMQ broker, specifying the broker URL, username, and password.

  9. Spring Boot JMS - Subscribe to durable topic with ActiveMQ

    Description: This query focuses on subscribing to a durable topic using Spring Boot JMS with ActiveMQ.

    @JmsListener(destination = "myTopic", containerFactory = "jmsListenerContainerFactory") public void receiveMessage(String message) { // Message handling logic } 

    Description: Use the @JmsListener annotation to define a method for receiving messages from a durable topic. Specify the topic name and the JmsListenerContainerFactory bean to use.


More Tags

google-cloud-sql docker-compose mediacontroller razor powershell-4.0 key-value-observing spring-data-elasticsearch vs-unit-testing-framework thread-local pythonpath

More Programming Questions

More Math Calculators

More Entertainment Anecdotes Calculators

More Mortgage and Real Estate Calculators

More Chemical thermodynamics Calculators