Accessing Azure Service Bus with Apache Camel?

柔情痞子 提交于 2019-12-10 18:06:26

问题


How can you access the Azure Service Bus using Apache Camel using Camel as a standalone Java application?


回答1:


If you are trying to access Azure Service Bus using Apache Camel you can do so by using the Camel AMQ libraries.

You can use the following Maven dependencies in case of you are using Maven:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-parent</artifactId>
            <version>2.17.6</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-amqp</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.qpid/qpid-amqp-1-0-client-jms -->
    <dependency>
        <groupId>org.apache.qpid</groupId>
        <artifactId>qpid-amqp-1-0-client-jms</artifactId>
        <version>0.32</version>
    </dependency>
</dependencies>

Note: I am using a relatively old version of Apache Camel here, but this setup should work with newer versions too.

If you want to create an endpoint for consuming messages from the Azure Service Bus you can create an AMQComponent which acts as a connection factory bind it to the registry and then use it to listen to messages.

Here is an example:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.amqp.AMQPComponent;
import org.apache.camel.main.Main;
import org.apache.qpid.amqp_1_0.jms.impl.ConnectionFactoryImpl;

public class AzureMQToFileAMQ {

    public static void main(String[] args) throws Exception {

        Main main = new Main();
        AMQPComponent connectionFactory = new AMQPComponent(
                ConnectionFactoryImpl
                        .createFromURL("amqps://"
                                + "dev.emea-uk-test.q:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@xxxxxxxxxxxxxxxxxxxxxxxxxxx.servicebus.windows.net"
                                + ":" + "5671"));
        main.bind("amqp", connectionFactory);
        main.addRouteBuilder(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("amqp:queue:dev.emea.uk.test.q?consumerType=Simple")
                        .process(exchange -> {
                            final String body = new String((byte[])exchange.getIn().getBody());
                            System.out.println(body);
                        });
            }
        });
        main.run();
    }
}


来源:https://stackoverflow.com/questions/50259714/accessing-azure-service-bus-with-apache-camel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!