使用Apache Camel将消息发送到Azure Servicebus队列

时间:2019-02-07 21:16:18

标签: apache-camel amqp azure-servicebus-queues

我需要将消息发送到Azure Service Bus中的队列。我当时使用HTTP Post发送该消息,但是我需要提高流量,然后决定测试AMQP协议。

代码下方:

public void configure() throws Exception {

    AMQPComponent amqp = AMQPComponent.amqpComponent("amqps://server.servicebus.windows.net",
                                "accessKey", "secretKey");

    getContext().addComponent("amqp", amqp);

    ActiveMQJMSConnectionFactory connection = new ActiveMQJMSConnectionFactory("tcp://localhost:61616",
                                "admin", "admin");

    getContext().addComponent("amq", JmsComponent.jmsComponent(connection));

    from("amq:TEST")
     .routeId("fromQueueToAzure")
     .autoStartup(true)
     .removeHeaders("JMS*")
     .to("amqp:amqp.queue")
     .log("sent");
}

当我开始这条路线时,通信正常,但是对于骆驼发送给Servicebus的每条消息,我都会得到以下日志:

2019-02-07 18:47:11 [main] INFO  DefaultCamelContext:3202 - Apache Camel 2.22.0 (CamelContext: camel-1) started in 0.602 seconds
2019-02-07 18:47:12 [AmqpProvider :(1):[amqps://server.servicebus.windows.net:-1]] INFO  SaslMechanismFinder:106 - Best match for SASL auth was: SASL-PLAIN
2019-02-07 18:47:12 [AmqpProvider :(1):[amqps://server.servicebus.windows.net:-1]] INFO  JmsConnection:1329 - Connection ID:5f75145f-6f10-4867-a590-782e507d51a8:1 connected to remote Broker: amqps://server.servicebus.windows.net
2019-02-07 18:47:13 [Camel (camel-1) thread #1 - JmsConsumer[TEST]] INFO  fromQueueToAzure:159 - sent
2019-02-07 18:47:14 [AmqpProvider :(2):[amqps://server.servicebus.windows.net:-1]] INFO  SaslMechanismFinder:106 - Best match for SASL auth was: SASL-PLAIN
2019-02-07 18:47:14 [AmqpProvider :(2):[amqps://server.servicebus.windows.net:-1]] INFO  JmsConnection:1329 - Connection ID:08ea246c-523e-4eb3-822e-c7d7b26aea85:2 connected to remote Broker: amqps://server.servicebus.windows.net
2019-02-07 18:47:15 [Camel (camel-1) thread #1 - JmsConsumer[TEST]] INFO  fromQueueToAzure:159 - sent
2019-02-07 18:47:16 [AmqpProvider :(3):[amqps://server.servicebus.windows.net:-1]] INFO  SaslMechanismFinder:106 - Best match for SASL auth was: SASL-PLAIN
2019-02-07 18:47:16 [AmqpProvider :(3):[amqps://server.servicebus.windows.net:-1]] INFO  JmsConnection:1329 - Connection ID:c8c40237-a73c-43cf-970d-c5cbf726eb21:3 connected to remote Broker: amqps://server.servicebus.windows.net
2019-02-07 18:47:17 [Camel (camel-1) thread #1 - JmsConsumer[TEST]] INFO  fromQueueToAzure:159 - sent

骆驼每条消息花费一秒钟的时间发送给Servicebus。 这是正常现象吗?是否可以使骆驼发送更快?

1 个答案:

答案 0 :(得分:1)

似乎每次发送时Camel路由都会创建一个新的Connection,从而可以解释为什么发送速度慢。为了提高性能,您需要使用PooledJMS之类的东西来创建连接池,这样就不会在每次发送时都重新创建新的连接和随附的资源。

您可以尝试以下类似方法,该方法使用camel-amqp组件,但直接将Qpid JMS与PooledJMS结合使用来进行配置。

    JmsConnectionFactory cf = new JmsConnectionFactory("amqp://localhost:5672");
    JmsPoolConnectionFactory pooledCF = new JmsPoolConnectionFactory();
    pooledCF.setConnectionFactory(cf);

    AMQPComponent component = new AMQPComponent();
    component.setConnectionFactory(pooledCF);

    CamelContext context = new DefaultCamelContext();
    context.addComponent("amqp", component);