【问题标题】:ActiveMQ Messages are not consumed spring boot, camelActiveMQ 消息不被消费 spring boot, camel
【发布时间】:2016-02-02 06:20:48
【问题描述】:

我正在尝试配置 Spring Boot、Apache Camel、ActiveMQ。这是我到目前为止所做的:

  1. 我使用 activemq.bat 运行 ActiveMQ
  2. 我登录控制台监控消息
  3. 我启动后端服务(来源如下)
  4. 我启动前端服务(后端和前端是不同的spring boot项目)
  5. 我成功地从前端向队列发送消息,但 20 秒后我超时。该消息显示在 ActiveMQ 控制台中,但不被后端使用。

这是我配置后端的方式:

build.gradle:

dependencies {
    compile("org.apache.camel:camel-spring-boot:2.16.0")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework:spring-messaging")
    compile("org.springframework:spring-jms")
    compile("org.springframework.security:spring-security-web")
    compile("org.springframework.security:spring-security-config")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile('org.apache.camel:camel-jms:2.16.0')
    compile("org.hibernate:hibernate-core:4.0.1.Final")
    compile("mysql:mysql-connector-java:5.1.37")
    compile("log4j:log4j:1.2.16")
    compile("junit:junit:4.12")
    compile("org.mockito:mockito-all:1.8.4")
    compile('org.apache.activemq:activemq-core:5.7.0')
    compile('com.epam.training.auction:auction_common:1.0')
    testCompile("junit:junit")
}

路由配置:(我使用 UsersServiceImpl 进行测试,两种定义方式都不起作用)

import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.epam.training.auction_backend.services.UsersServiceImpl;

@Configuration
public class MyRouterConfiguration {
  @Bean
  public RoutesBuilder myRouter() {
    return new RouteBuilder() {

      @Override
      public void configure() throws Exception {
          from("jms:queue:auctions").to("bean:auctionsServiceImpl");
          from("jms:queue:users").bean(UsersServiceImpl.class);
          from("jms:queue:bidding").to("bean:biddingServiceImpl");
      }
    };
  }
}

客户端,调用方法

@Override
public void registerUser(String username, String password) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("camel-client-remoting.xml");
    UsersService usersService = context.getBean("usersServiceImpl", UsersService.class);

    System.out.println("Invoking the logging");
    UserTransferObject userTransferObject = new UserTransferObject("user", "pass");
    usersService.addUser(userTransferObject);
    System.out.println("User is logged");

    IOHelper.close(context);
}

客户端xml骆驼配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camel:camelContext id="camel-client">
    <camel:template id="camelTemplate"/>

    <camel:proxy
        id="auctionsServiceImpl"
        serviceInterface="com.epam.training.auction.common.AuctionsService"
        serviceUrl="jms:queue:auctions"/>

    <camel:proxy
        id="usersServiceImpl"
        serviceInterface="com.epam.training.auction.common.UsersService"
        serviceUrl="jms:queue:users"/>

    <camel:proxy
        id="biddingServiceImpl"
        serviceInterface="com.epam.training.auction.common.BiddingService"
        serviceUrl="jms:queue:bidding"/>
  </camel:camelContext>

  <bean id="jmsConnectionFactory"
        class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616"/>
  </bean>

  <bean id="pooledConnectionFactory"
        class="org.apache.activemq.pool.PooledConnectionFactory"
        init-method="start" destroy-method="stop">
    <property name="maxConnections" value="8"/>
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
  </bean>

  <bean id="jmsConfig"
        class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory"/>
    <property name="concurrentConsumers" value="10"/>
  </bean>

  <bean id="jms"
        class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig"/>
    <property name="transacted" value="true"/>
    <property name="cacheLevelName" value="CACHE_CONSUMER"/>
  </bean>

</beans>

在发送过程中我也收到警告:

2015-11-02 11:56:21.547  WARN 16328 --- [nio-8181-exec-5] o.s.b.f.s.DefaultListableBeanFactory     : Bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersServiceImpl': Invocation of init method failed; nested exception is org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: jms://queue:users due to: Cannot auto create component: jms

公共接口和传输对象在第三个项目中定义,它是后端和前端项目的依赖项。

我觉得这个配置缺少一个部分。请告诉我可能是什么。

提前致谢。

【问题讨论】:

    标签: spring spring-boot apache-camel activemq


    【解决方案1】:

    你需要改变

    <bean id="activemq"
        class="org.apache.activemq.camel.component.ActiveMQComponent">
    

    <bean id="jms"
        class="org.apache.activemq.camel.component.ActiveMQComponent">
    

    或将您的端点网址更改为.to("activemq:queue:users")

    您的 ActiveMQComponent 的 id 是 .to() 中使用的名称,用于向骆驼标识您要使用该组件定义。

    【讨论】:

    • 感谢您的回答。不幸的是,它没有帮助。我曾经在服务器端得到“可能的模糊方法调用”,但我无法重现它。可能这不是领先一步,只是没有连接错误。后端不做任何事情,消息被发布并超时。
    • 您能否用您所做的更改更新您的原始问题,以便我查看是否有遗漏的内容?
    • activemq实例是否配置了鉴权?如果是这样,那么 jmsConnectionFactory 将需要用户名和密码才能连接。你查看过activemq日志吗?
    • 没有密码。尤其是前端可以在不提供任何东西的情况下连接到它。
    • wklej.org/id/1832124 - 来自 activemq 的日志 wklej.org/id/1832126 - 来自包装器的日志 仅开始日志。这就是我得到的例外:wklej.org/id/1832128
    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2016-12-09
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2021-03-06
    相关资源
    最近更新 更多