Home:ALL Converter>Subscriber-app not listening to messages on ActiveMQ using topic

Subscriber-app not listening to messages on ActiveMQ using topic

Ask Time:2021-01-09T12:56:57         Author:Abhishek Madhusudhan

Json Formatter

I am trying to bootstrap a small subscriber-app using gradle and springboot to listen to messages posted to a topic on ActiveMQ. I observe that whenever I try to run the app - it fails to register itself as a subscriber on ActiveMQ and listen to the messages being posted to the topic. The process terminates without a stack-trace. However, a similar codebase on github (Reference-2) but with a maven build structure seems to connect and receive the posted topic messages on ActiveMQ (localhost). Having debugged the issue for long - I have not been able to spot the root cause. Following are code snippets for reference:-

Any input would be appreciated. Thank You!

Note: All runs are made from within eclipse under Java 1.8 environment.

JMSTopicSubscriber.java

@Component
public class JMSTopicSubscriber
{
   @JmsListener(destination = "${jms.enrol.topic.name}")
   public void receiveMessage(Exam exam)
   {
      System.out.println("Received message: " + exam.toString());
   }
}

JMSConfig.java

@EnableJms
@Configuration
@ComponentScan(basePackages = { "experiment.jms.subscriber" })
@PropertySource("classpath:application.properties")
public class JMSConfig
{
   @Value("${spring.activemq.broker-url}")
   String m_activeMQBrokerUrl;

   @Bean
   public ConnectionFactory connectionFactory()
   {
      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
      cf.setBrokerURL(m_activeMQBrokerUrl);
      return cf;
   }

   @Bean
   public JmsListenerContainerFactory<?> jmsListenerContainerFactory()
   {
      DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); 
      factory.setPubSubDomain(true);
      factory.setMessageConverter(messageConverter());
      factory.setConnectionFactory(connectionFactory()); 
      return factory; 
   }
   
   @Bean
   public MessageConverter messageConverter()
   {
       MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
       converter.setTargetType(MessageType.TEXT);
       converter.setTypeIdPropertyName("_type");
       return converter;
   }
}

ExperimentTopicJmsSubscriberApplication.java

@SpringBootApplication
@ComponentScan(basePackages = { "experiment.jmsSubscriber" })
public class ExperimentTopicJmsSubscriberApplication
{
   public static void main(String[] args)
   {
      SpringApplication.run(ExperimentTopicJmsSubscriberApplication.class, args);
   }
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'experiment.jms.topic'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
    // ActiveMQ Dependencies. 
    implementation 'org.springframework.boot:spring-boot-starter-activemq'
}

test {
    useJUnitPlatform()
}

application.properties

spring.activemq.broker-url = tcp://localhost:61616

#topic name
jms.enrol.topic.name = allexams

spring.jms.pub-sub-domain=true

##Configure concurrent listeners using the spring.jms.concurrency and spring.jms.max-concurrency properties.
#spring.jms.concurrency property configures the minimum number of concurrent consumers.
spring.jms.listener.concurrency=2

#spring.jms.max-concurrency configures the maximum number of concurrent consumers.
spring.jms.listener.max-concurrency=2

Package Structure

enter image description here

Data Model

    public class Exam implements Serializable
    {
       private String m_examName; 
       private int m_examYear; 
       
       public Exam()
       {
          
       }
       
       public Exam( String examName, int examYear )
       {
          this.m_examName = examName; 
          this.m_examYear = examYear;  
       }
       
       public String getExamName()
       {
          return this.m_examName; 
       }
       
       public int getExamYear()
       {
          return this.m_examYear;
       }
       
       public String toString()
       {
          return "(Exam Name: " + this.m_examName + ", "
                + "Exam Year: " + Integer.toString(this.m_examYear) + ")"; 
       }
     }

References

  1. https://github.com/smitha-madhavamurthy/springboot-activemq-subscriber
  2. https://grokonez.com/java-integration/activemq-work-spring-jms-activemq-topic-publisher-subcribers-pattern-using-springboot

Author:Abhishek Madhusudhan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65639490/subscriber-app-not-listening-to-messages-on-activemq-using-topic
yy