骆驼在许多路线中使用相同的JMS组件

时间:2019-06-13 23:22:42

标签: spring-boot apache-camel activemq-artemis

我正在尝试配置3个骆驼路线以使用相同的JMS组件。

问题是当我“必须”在这些不同的路线中使用相同的组件时。 当我这样做时,有时会显示错误消息,提示无法连接到ActiveMQ。 这是因为并发吗? 如果可以,我想知道是否可以立即配置此组件并在没有并发的情况下使用它。

当我用不同的名称定义组件时,它可以工作。 例如:amq1,amq2和amq3。

我正在使用:

  • Apache Camel: 2.21
  • ActiveMQ Artemis: 1.5.6
  • Springboot: v1.5.16.RELEASE

代码: 创建JMS组件的方法:

  public JmsComponent createComponent() {
    private ActiveMQJMSConnectionFactory connection;
    connection = new ActiveMQJMSConnectionFactory(host, user, password);
    connection.setMinLargeMessageSize(10000000);

    return JmsComponent.jmsComponent(connection);
  }

添加要路由的组件:

路线1:

@Component
public class Route1 extends RouteBuilder {

  @Override
  public void configure() throws Exception {

    if (!getContext().getComponentNames().contains("amq")) {
      getContext().addComponent("amq", createComponent());
    }
    from("amq:" + queue)
      .routeId("Route1")
      .to("mock:result");
  }
}

路线2:

@Component
public class Route2 extends RouteBuilder {

  @Override
  public void configure() throws Exception {

    if (!getContext().getComponentNames().contains("amq")) {
      getContext().addComponent("amq", createComponent());
    }
    from("amq:" + queue)
      .routeId("Route2")
      .to("mock:result");
  }
}

路线3:

@Component
public class Route3 extends RouteBuilder {

  @Override
  public void configure() throws Exception {

    if (!getContext().getComponentNames().contains("amq")) {
      getContext().addComponent("amq", createComponent());
    }

    from("timer:Test?period=2s")
      .routeId("Route3")
      .setBody(simple("test"))
      .to("amq:" + queue);
  }
}

AMQ Broker接受器:

      <acceptors>

         <acceptor name="artemis">tcp://0.0.0.0:61616?connectionTtl=60000;needClientAuth=true;supportAdvisory=false;anycastPrefix=jms.queue.;multicastPrefix=jms.topic.;tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=CORE,AMQP,STOMP,HORNETQ,MQTT,OPENWIRE;useEpoll=true;amqpCredits=1000;amqpLowCredits=300</acceptor>

         <acceptor name="amqp">tcp://0.0.0.0:5672?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=AMQP;useEpoll=true;amqpCredits=1000;amqpLowCredits=300</acceptor>

         <acceptor name="stomp">tcp://0.0.0.0:61613?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=STOMP;useEpoll=true</acceptor>

         <acceptor name="hornetq">tcp://0.0.0.0:5445?anycastPrefix=jms.queue.;multicastPrefix=jms.topic.;protocols=HORNETQ,STOMP;useEpoll=true</acceptor>

         <acceptor name="mqtt">tcp://0.0.0.0:1883?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=MQTT;useEpoll=true</acceptor>

      </acceptors>

错误:

Error processing exchange. Exchange[ID-op20190003-01-valenet-18-wlgfm-1560467581146-0-2]. Caused by: [org.springframework.jms.UncategorizedJmsException - Uncategorized exception occurred during JMS processing; nested exception is javax.jms.JMSException: Failed to create session factory; nested exception is ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ119007: Cannot connect to server(s). Tried with all available servers.]]
org.springframework.jms.UncategorizedJmsException: Uncategorized exception occurred during JMS processing; nested exception is javax.jms.JMSException: Failed to create session factory; nested exception is ActiveMQNotConnectedException[errorType=NOT_CONNECTED message=AMQ119007: Cannot connect to server(s). Tried with all available servers.]
    at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:316)
    at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:169)
    at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:487)
    at org.apache.camel.component.jms.JmsConfiguration$CamelJmsTemplate.send(JmsConfiguration.java:526)
    at org.apache.camel.component.jms.JmsProducer.doSend(JmsProducer.java:440)
    at org.apache.camel.component.jms.JmsProducer.processInOnly(JmsProducer.java:394)
    at org.apache.camel.component.jms.JmsProducer.process(JmsProducer.java:157)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.direct.DirectBlockingProducer.process(DirectBlockingProducer.java:53)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.direct.DirectBlockingProducer.process(DirectBlockingProducer.java:53)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197)
    at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:79)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: javax.jms.JMSException: Failed to create session factory
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:757)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:265)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:260)
    at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:180)
    at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:474)
    ... 26 common frames omitted
Caused by: org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException: AMQ119007: Cannot connect to server(s). Tried with all available servers.
    at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:784)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:755)
... 30 common frames omitted

1 个答案:

答案 0 :(得分:1)

您可以通过扩展DefaultCamelContext来创建自己的CamelContext,并在那里仅创建一次组件。

@Component
class YourCamelContext extends DefaultCamelContext {
  @PostConstruct
  private void addStuff() {
    addComponent("amq", createComponent());
...