问题
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