如何以独立Java应用形式使用Apache Camel访问Azure Service Bus?
嘿,我来给你一步步拆解怎么用Apache Camel搭建独立Java应用来访问Azure Service Bus——我之前在项目里这么做过,踩过几个版本兼容的小坑,分享给你:
1. 添加必要的Maven依赖
首先得把Camel核心、Azure Service Bus组件,还有用于启动独立应用的Camel Main组件加进来。注意版本要匹配,比如Camel 3.x对应Azure SDK 7.x,Camel 4.x可以用更新的Azure SDK版本:
<dependencies> <!-- Camel核心依赖 --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>3.20.2</version> <!-- 替换为最新稳定版 --> </dependency> <!-- Camel Azure Service Bus组件 --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-azure-servicebus</artifactId> <version>3.20.2</version> </dependency> <!-- Camel Main:用于启动独立Camel应用 --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-main</artifactId> <version>3.20.2</version> </dependency> <!-- Azure Service Bus客户端依赖 --> <dependency> <groupId>com.azure</groupId> <artifactId>azure-messaging-servicebus</artifactId> <version>7.17.0</version> <!-- 与Camel组件兼容的版本 --> </dependency> </dependencies>
2. 配置Azure Service Bus连接信息
把你的Service Bus命名空间连接字符串、队列/主题名称放到application.properties里(放在src/main/resources下),这样不用硬编码,方便后续修改:
# Azure Service Bus连接字符串 azure.servicebus.connection-string=Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=your-access-key= # 要访问的队列/主题名称 azure.servicebus.target-name=your-queue-or-topic-name
3. 编写发送消息的Camel路由
用Camel Main类启动应用,创建一个定时发送消息的路由,示例如下:
import org.apache.camel.Main; import org.apache.camel.builder.RouteBuilder; public class ServiceBusMessageSender { public static void main(String[] args) throws Exception { Main camelMain = new Main(); // 加载配置文件 camelMain.configure().addPropertiesSource("classpath:application.properties"); // 添加发送消息的路由 camelMain.addRouteBuilder(new RouteBuilder() { @Override public void configure() throws Exception { // 每10秒发送一条带时间戳的测试消息 from("timer:sendTimer?period=10000") .setBody(simple("Camel test message sent at ${date:now:yyyy-MM-dd HH:mm:ss}")) .to("azure-servicebus://{{azure.servicebus.target-name}}?connectionString={{azure.servicebus.connection-string}}") .log("Message sent to Azure Service Bus: ${body}"); } }); // 启动Camel应用 camelMain.run(args); } }
4. 编写接收消息的Camel路由
同样用Camel Main,创建一个监听Service Bus队列/主题的路由,处理收到的消息:
import org.apache.camel.Main; import org.apache.camel.builder.RouteBuilder; public class ServiceBusMessageReceiver { public static void main(String[] args) throws Exception { Main camelMain = new Main(); camelMain.configure().addPropertiesSource("classpath:application.properties"); camelMain.addRouteBuilder(new RouteBuilder() { @Override public void configure() throws Exception { // 监听Azure Service Bus队列 from("azure-servicebus://{{azure.servicebus.target-name}}?connectionString={{azure.servicebus.connection-string}}&consumerType=client") .log("Received message from Service Bus: ${body}") // 调用自定义处理器处理消息 .bean(MessageProcessor.class, "process"); } }); camelMain.run(args); } // 自定义消息处理器 public static class MessageProcessor { public void process(String messageContent) { // 这里可以写你的业务逻辑:比如存数据库、调用API等 System.out.println("Processing message content: " + messageContent); } } }
几个关键注意事项
- 版本兼容:一定要确保Camel组件和Azure SDK的版本匹配,不然会出现奇怪的依赖冲突或者API不兼容问题,建议查Camel官方文档的Azure Service Bus组件页面确认兼容版本。
- 消费者类型:如果你的队列是会话队列,要把
consumerType设置为session,默认是client(普通消费者)。 - 消息确认:默认是自动确认消息,如果你需要手动控制确认(比如处理完业务逻辑再确认),可以设置
autoComplete=false,然后在路由里调用ack()方法。 - 主题订阅:如果访问的是主题而不是队列,只需要把路由里的目标名称换成主题名,然后可以通过
subscriptionName参数指定订阅名,比如:from("azure-servicebus://your-topic?connectionString=xxx&subscriptionName=your-subscription")
完整的双向交互示例
如果你想在同一个应用里既发送又接收消息,直接把两个路由加到同一个Main类里就行:
import org.apache.camel.Main; import org.apache.camel.builder.RouteBuilder; public class CamelServiceBusFullApp { public static void main(String[] args) throws Exception { Main camelMain = new Main(); camelMain.configure().addPropertiesSource("classpath:application.properties"); camelMain.addRouteBuilder(new RouteBuilder() { @Override public void configure() throws Exception { // 发送路由 from("timer:sendTimer?period=10000") .setBody(simple("Bidirectional test message: ${date:now:HH:mm:ss}")) .to("azure-servicebus://{{azure.servicebus.target-name}}?connectionString={{azure.servicebus.connection-string}}") .log("Sent: ${body}"); // 接收路由 from("azure-servicebus://{{azure.servicebus.target-name}}?connectionString={{azure.servicebus.connection-string}}") .log("Received: ${body}") .bean(MessageProcessor.class, "process"); } }); camelMain.run(args); } public static class MessageProcessor { public void process(String msg) { System.out.println("Handled message: " + msg); } } }
内容的提问来源于stack exchange,提问作者gil.fernandes
相关产品推荐
相关产品推荐

