简体   繁体   中英

JMS not starting in Spring Boot application

I developed a spring boot application which send and listen message in Activemq using JMS, but while running application, JMS is not getting started by spring boot

This is mainclass, Application.java

@SpringBootApplication
@EnableJms
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

Configuration class: Config.java

@Configuration
public class Config {

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("inmemory.queue");
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        return new JmsTemplate(activeMQConnectionFactory());
    }
}

Listener class is used to listen to the queue: Listener.java

@Component
public class Listener {

    @JmsListener(destination = "inmemory.queue")
    public void listener(String message) {
        System.out.println("message received" + message);
    }
}

Producer class is used to send message to queue from controller: Producer.java

@RestController
public class Producer {

    @Autowired
    Queue queue;

    @Autowired
    JmsTemplate jmstemplate;

    @RequestMapping(method = RequestMethod.GET, path = "/test3/{message}")
    public String test3(@PathVariable String message) {
        jmstemplate.convertAndSend(queue, message);
        return "teste3" + message;
    }
}

application.properties

spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
server.port=8081

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.6.RELEASE</version>
   </parent>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <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.springframework</groupId>
         <artifactId>spring-jms</artifactId>
         <version>5.1.4.RELEASE</version>
      </dependency>
   </dependencies>
</project>

This is a screenshot of application starting without starting JMS

Please use below code for jmsTemplate(...) method.

@Bean
public JmsTemplate jmsTemplate() {
    return new JmsTemplate(new ActiveMQConnectionFactory("vm://localhost"));
}

I have tested and it is working, let me know if you need sample code.

Also, no need to install ActiveMQ in local when you are using a starter .

Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-09-16 12:39:15.477  INFO 14111 --- [           main] c.e.activeissue.ActiveIssueApplication   : Starting ActiveIssueApplication on kode12-B250M-D3H with PID 14111 (/home/yprajapati/Downloads/active-issue/target/classes started by yprajapati in /home/yprajapati/Downloads/active-issue)
2019-09-16 12:39:15.493  INFO 14111 --- [           main] c.e.activeissue.ActiveIssueApplication   : No active profile set, falling back to default profiles: default
2019-09-16 12:39:16.895  INFO 14111 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-09-16 12:39:16.919  INFO 14111 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-16 12:39:16.919  INFO 14111 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-09-16 12:39:16.991  INFO 14111 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-16 12:39:16.991  INFO 14111 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1407 ms
2019-09-16 12:39:17.204  INFO 14111 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-16 12:39:17.440  INFO 14111 --- [           main] o.apache.activemq.broker.BrokerService   : Using Persistence Adapter: MemoryPersistenceAdapter
2019-09-16 12:39:17.505  INFO 14111 --- [  JMX connector] o.a.a.broker.jmx.ManagementContext       : JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
2019-09-16 12:39:17.574  INFO 14111 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:kode12-B250M-D3H-39191-1568617757456-0:1) is starting
2019-09-16 12:39:17.577  INFO 14111 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:kode12-B250M-D3H-39191-1568617757456-0:1) started
2019-09-16 12:39:17.577  INFO 14111 --- [           main] o.apache.activemq.broker.BrokerService   : For help or more information please see: http://activemq.apache.org
2019-09-16 12:39:17.594  INFO 14111 --- [           main] o.a.activemq.broker.TransportConnector   : Connector vm://localhost started
2019-09-16 12:39:17.734  INFO 14111 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-09-16 12:39:17.737  INFO 14111 --- [           main] c.e.activeissue.ActiveIssueApplication   : Started ActiveIssueApplication in 2.872 seconds (JVM running for 3.326)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM