【问题标题】:JMS not starting in Spring Boot applicationJMS 未在 Spring Boot 应用程序中启动
【发布时间】:2020-01-16 09:39:07
【问题描述】:

我开发了一个 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 类用于从控制器向队列发送消息: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

【问题讨论】:

  • 您好,显示的错误是什么?你能补充一下你的问题吗?
  • 没有错误,我是spring boot的新手。我正在使用eclipse。问题是程序正在运行,但 JMS 没有启动
  • 请提供您的 pom.xml
  • @YogeshPrajapati pom.xml 添加..请帮助
  • @JonathanJohx .. 请看看

标签: spring-boot jms activemq


【解决方案1】:

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

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

我已经测试过了,如果你需要示例代码,请告诉我。

另外,当您使用 starter 时,无需在本地安装 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)

【讨论】:

  • 对我来说显示错误:无法刷新目标“inmemory.queue”的 JMS 连接 - 使用 FixedBackOff{interval=5000, currentAttempts=5, maxAttempts=unlimited} 重试。原因:无法创建传输。原因:java.io.IOException:无法识别传输方案:[vm
  • 在这种情况下,请在您的问题中提供 activeMQConnectionFactory() 方法的实现。
猜你喜欢
  • 2016-09-10
  • 2022-10-20
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 2016-01-21
  • 2017-07-17
  • 1970-01-01
相关资源
最近更新 更多