JMS没有在Spring Boot应用程序中启动

时间:2019-09-15 06:48:25

标签: spring-boot jms activemq

我开发了一个Spring Boot应用程序,该应用程序使用JMS在Activemq中发送和侦听消息,但是在运行应用程序时,JMS不能通过Spring Boot来启动

这是主类Application.java

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

配置类:Config.java

@Configuration
public class Config {

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

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

Listener类用于侦听队列:Listener.java

@Component
public class Listener {

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

生产者类用于将消息从控制器发送到队列: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

1 个答案:

答案 0 :(得分:0)

请对 jmsTemplate(...) 方法使用以下代码。

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

我已经测试过并且可以正常工作,如果需要示例代码,请告诉我。

此外,在使用启动器时,无需在本地安装 ActiveMQ

输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)