javaspringintellij-ideamessage-queuespring-jms

Can't start Listener Java Application using ActiveMQ in IntelliJ


I have a Java MQ Listener Application that listens a Queue. When I use it locally, I use ActiveMQ to send messages, my issue is:

I am having a problem when trying to run this Listener Application in IntelliJ using ActiveMQ. When I use Eclipse using ActiveMQ, it works perfectly, it is the same application, I think it is something related with my IntelliJ messaging configuration.

This is my JmsConfig class:

package br.com.company.hub.mq.app.jms;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.component.jms.JmsComponent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;

import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsConstants;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.common.CommonConstants;

/** Configuration class for creating mq connection and jms component */

@Configuration
public class JmsConfig {

  private String host;
  private Integer port;
  private String queueManager;
  private String channel;

  public JmsConfig(@Value("${queue.host}") String host, @Value("${queue.port}") Integer port,
      @Value("${queue.queue-manager}") String queueManager, @Value("${queue.channel}") String channel) {

    this.host = host;
    this.port = port;
    this.queueManager = queueManager;
    this.channel = channel;
  }

  @Bean("jmsConnectionFactory")
  @ConditionalOnProperty(value = "is-localhost", havingValue = "true", matchIfMissing = false)
  public ConnectionFactory activeMqConnectionFactory() {
    return new ActiveMQConnectionFactory("tcp://localhost:61616");
  }

  @Bean("jmsConnectionFactory")
  @ConditionalOnProperty(value = "is-localhost", havingValue = "false", matchIfMissing = false)
  public ConnectionFactory ibmMqConnectionFactory() throws JMSException {

    final JmsFactoryFactory ff = JmsFactoryFactory.getInstance(JmsConstants.WMQ_PROVIDER);
    final JmsConnectionFactory cf = ff.createConnectionFactory();

    cf.setStringProperty(CommonConstants.WMQ_HOST_NAME, this.host);
    cf.setStringProperty(CommonConstants.WMQ_QUEUE_MANAGER, this.queueManager);
    cf.setStringProperty(CommonConstants.WMQ_CHANNEL, this.channel);
    cf.setIntProperty(CommonConstants.WMQ_PORT, this.port);
    cf.setIntProperty(CommonConstants.WMQ_CONNECTION_MODE, CommonConstants.WMQ_CM_BINDINGS_THEN_CLIENT);
    cf.setIntProperty(CommonConstants.WMQ_CLIENT_RECONNECT_OPTIONS, CommonConstants.WMQ_CLIENT_RECONNECT_Q_MGR);

    return cf;
  }

  @Bean
  public JmsTemplate jmsTemplate(final ConnectionFactory jmsConnectionFactory) {
    return new JmsTemplate(jmsConnectionFactory);
  }


  @Bean(name = "wmq")
  public JmsComponent jmsComponent(final ConnectionFactory jmsConnectionFactory) {
    return JmsComponent.jmsComponent(jmsConnectionFactory);
  }
}

This is the ActiveMQ dependency:

    <!-- MQ Local Tests -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-client</artifactId>
        <version>5.16.7</version>
        <scope>provided</scope>     
    </dependency>

This is the error when I run it:

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.4)

14:26:09.702[-preinit] DEBUG                        org.jboss.logging  :  :  : Logging Provider: org.jboss.logging.Slf4jLoggerProvider found via system property
14:26:09.706[-preinit] INFO       o.h.validator.internal.util.Version  :  :  : HV000001: Hibernate Validator 8.0.1.Final
14:26:09.795[    main] INFO  .c.s.j.c.AppMQ  :  :  : Starting AppMQ using Java 17.0.9 with PID 12900 (C:\Repositories\app-mq-v1\target\classes started by xx in C:\Users\xxx\IdeaProjects\untitled)
14:26:09.795[    main] DEBUG .c.s.j.c.AppMQ  :  :  : Running with Spring Boot v3.2.4, Spring v6.1.5
14:26:09.797[    main] INFO  .c.s.j.c.AppMQ  :  :  : The following 1 profile is active: "dev"
14:26:11.960[    main] TRACE   o.s.c.c.CacheOrGroupedOpenApiCondition  :  :  : Condition CacheOrGroupedOpenApiCondition on org.springdoc.core.configuration.SpringDocConfiguration#springdocBeanFactoryPostProcessor did not match due to AnyNestedCondition 0 matched 2 did not; NestedCondition on CacheOrGroupedOpenApiCondition.OnCacheDisabled found non-matching nested conditions @ConditionalOnProperty (springdoc.cache.disabled) did not find property 'springdoc.cache.disabled'; NestedCondition on CacheOrGroupedOpenApiCondition.OnMultipleOpenApiSupportCondition AnyNestedCondition 0 matched 2 did not; NestedCondition on MultipleOpenApiSupportCondition.OnActuatorDifferentPort found non-matching nested conditions @ConditionalOnProperty (springdoc.show-actuator) did not find property 'springdoc.show-actuator'; NestedCondition on MultipleOpenApiSupportCondition.OnMultipleOpenApiSupportCondition AnyNestedCondition 0 matched 2 did not; NestedCondition on MultipleOpenApiGroupsCondition.OnGroupConfigProperty @ConditionalOnProperty (springdoc.group-configs[0].group) did not find property 'springdoc.group-configs[0].group'; NestedCondition on MultipleOpenApiGroupsCondition.OnGroupedOpenApiBean @ConditionalOnBean (types: org.springdoc.core.models.GroupedOpenApi; SearchStrategy: all) did not find any beans of type org.springdoc.core.models.GroupedOpenApi
14:26:11.993[    main] TRACE  o.s.c.c.MultipleOpenApiSupportCondition  :  :  : Condition MultipleOpenApiSupportCondition on org.springdoc.webmvc.core.configuration.MultipleOpenApiSupportConfiguration did not match due to AnyNestedCondition 0 matched 2 did not; NestedCondition on MultipleOpenApiSupportCondition.OnActuatorDifferentPort found non-matching nested conditions @ConditionalOnProperty (springdoc.show-actuator) did not find property 'springdoc.show-actuator'; NestedCondition on MultipleOpenApiSupportCondition.OnMultipleOpenApiSupportCondition AnyNestedCondition 0 matched 2 did not; NestedCondition on MultipleOpenApiGroupsCondition.OnGroupConfigProperty @ConditionalOnProperty (springdoc.group-configs[0].group) did not find property 'springdoc.group-configs[0].group'; NestedCondition on MultipleOpenApiGroupsCondition.OnGroupedOpenApiBean @ConditionalOnBean (types: org.springdoc.core.models.GroupedOpenApi; SearchStrategy: all) did not find any beans of type org.springdoc.core.models.GroupedOpenApi
14:26:13.741[    main] INFO  o.s.b.w.e.j.JettyServletWebServerFactory  :  :  : Server initialized with port: 7800
14:26:13.744[    main] INFO           org.eclipse.jetty.server.Server  :  :  : jetty-12.0.7; built: 2024-02-29T21:19:41.771Z; git: c89aca8fd34083befd79f328a3b8b6ffff04347e; jvm 17.0.9+11-LTS-201
14:26:13.810[    main] INFO      o.e.j.s.h.ContextHandler.application  :  :  : Initializing Spring embedded WebApplicationContext
14:26:13.810[    main] INFO  w.s.c.ServletWebServerApplicationContext  :  :  : Root WebApplicationContext: initialization completed in 3927 ms
14:26:14.342[    main] INFO     o.e.j.session.DefaultSessionIdManager  :  :  : Session workerName=node0
14:26:14.371[    main] INFO   o.e.jetty.server.handler.ContextHandler  :  :  : Started osbwej.JettyEmbeddedWebAppContext@24d7657b{application,/,b=[file:/C:/Users/xxx/AppData/Local/Temp/jetty-docbase.7800.15218493539891600500/, jar:file:///C:/Users/xxx/.m2/repository/org/webjars/swagger-ui/4.18.2/swagger-ui-4.18.2.jar!/META-INF/resources/],a=AVAILABLE,h=oeje10s.SessionHandler@72e49f6a{STARTED}}
14:26:14.372[    main] INFO     o.e.j.e.servlet.ServletContextHandler  :  :  : Started osbwej.JettyEmbeddedWebAppContext@24d7657b{application,/,b=[file:/C:/Users/xxx/AppData/Local/Temp/jetty-docbase.7800.15218493539891600500/, jar:file:///C:/Users/xxx/.m2/repository/org/webjars/swagger-ui/4.18.2/swagger-ui-4.18.2.jar!/META-INF/resources/],a=AVAILABLE,h=oeje10s.SessionHandler@72e49f6a{STARTED}}
14:26:14.378[    main] INFO           org.eclipse.jetty.server.Server  :  :  : Started oejs.Server@5072e638{STARTING}[12.0.7,sto=0] @7290ms
14:26:15.040[    main] WARN  ConfigServletWebServerApplicationContext  :  :  : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsConnectionFactory' defined in class path resource [br/com/company/hub/mq/app/jms/JmsConfig.class]: Failed to instantiate [javax.jms.ConnectionFactory]: Factory method 'activeMqConnectionFactory' threw exception with message: org/apache/activemq/ActiveMQConnectionFactory
14:26:15.055[    main] INFO           org.eclipse.jetty.server.Server  :  :  : Stopped oejs.Server@5072e638{STOPPING}[12.0.7,sto=0]
14:26:15.060[    main] INFO     o.e.j.e.servlet.ServletContextHandler  :  :  : Stopped osbwej.JettyEmbeddedWebAppContext@24d7657b{application,/,b=[file:/C:/Users/xxx/AppData/Local/Temp/jetty-docbase.7800.15218493539891600500/, jar:file:///C:/Users/xxx/.m2/repository/org/webjars/swagger-ui/4.18.2/swagger-ui-4.18.2.jar!/META-INF/resources/],a=AVAILABLE,h=oeje10s.SessionHandler@72e49f6a{STOPPED}}
14:26:15.086[    main] INFO  .s.b.a.l.ConditionEvaluationReportLogger  :  :  : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
14:26:15.116[    main] ERROR               o.s.boot.SpringApplication  :  :  : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsConnectionFactory' defined in class path resource [br/com/company/hub/mq/app/jms/JmsConfig.class]: Failed to instantiate [javax.jms.ConnectionFactory]: Factory method 'activeMqConnectionFactory' threw exception with message: org/apache/activemq/ActiveMQConnectionFactory
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1165)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
    at br.com.company.jlcaml.clientintegration.AppMQ.main(AppMQ.java:12)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.jms.ConnectionFactory]: Factory method 'activeMqConnectionFactory' threw exception with message: org/apache/activemq/ActiveMQConnectionFactory
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:177)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
    ... 19 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/activemq/ActiveMQConnectionFactory
    at br.com.company.hub.mq.app.jms.JmsConfig.activeMqConnectionFactory(JmsConfig.java:41)
    at br.com.company.hub.mq.app.jms.JmsConfig$$SpringCGLIB$$0.CGLIB$activeMqConnectionFactory$2(<generated>)
    at br.com.company.hub.mq.app.jms.JmsConfig$$SpringCGLIB$$FastClass$$1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at br.com.company.hub.mq.app.jms.JmsConfig$$SpringCGLIB$$0.activeMqConnectionFactory(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1165)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
Caused by: java.lang.ClassNotFoundException: org.apache.activemq.ActiveMQConnectionFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at br.com.company.hub.mq.app.jms.JmsConfig.activeMqConnectionFactory(JmsConfig.java:41)
    at br.com.company.hub.mq.app.jms.JmsConfig$$SpringCGLIB$$0.CGLIB$activeMqConnectionFactory$2(<generated>)
    at br.com.company.hub.mq.app.jms.JmsConfig$$SpringCGLIB$$FastClass$$1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at br.com.company.hub.mq.app.jms.JmsConfig$$SpringCGLIB$$0.activeMqConnectionFactory(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1165)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)

Process finished with exit code 1```

The focus is to discover which is the difference between IntelliJ and Eclipse when running this app. 

I tryed to install and uninstall IntelliJ several times, I also tryed to install a [JMS Messenger plugin](https://plugins.jetbrains.com/plugin/10949-jms-messenger) in IntelliJ but didn't work.

Solution

  • Perhaps to do the with the way the compiled application is assembled and then run.

    In this:

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
            <version>5.16.7</version>
            <scope>provided</scope>     
        </dependency>
    

    remove <scope>provided</scope>

    reason: provided:

    indicates you expect the JDK or a container to provide the dependency at runtime

    and I imagine Eclipse is providing this for you. Removing that keyword and the default scope (compile) will be used and this dependency will then be made available when you run the application

    See https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope