Spring JMS with Azure Service Bus
2016-07-02 17:55
706 查看
Spring JMS 整合Azure Service Bus,JMS API实现 依然借助于Qpid JMS Client的实现。
源码下载地址:spring-jms-servicebus-demo
Qpid JMS Client 连接 Azure Service Bus 的文档,请参考:
http://arui.me/index.php/archives/120/
首先,创建Maven 项目,将Spring JMS和Qpid的依赖加入到POM中。
之后,设置配置文件,并通过Spring读取配置文件
代码示例
源码下载地址:spring-jms-servicebus-demo
Qpid JMS Client 连接 Azure Service Bus 的文档,请参考:
http://arui.me/index.php/archives/120/
首先,创建Maven 项目,将Spring JMS和Qpid的依赖加入到POM中。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.apache.qpid</groupId> <artifactId>qpid-jms-client</artifactId> <version>0.9.0</version> </dependency>
之后,设置配置文件,并通过Spring读取配置文件
servicebus.hostname=amqps://kevinsb.servicebus.chinacloudapi.cn?amqp.idleTimeout=200000 servicebus.username=RootManageSharedAccessKey servicebus.password=n6Kh4vcfEpSjD0oAZrmPtNF1oFEoVLsOra4FB4c36UM= servicebus.queue=queue1
代码示例
@Configuration @ComponentScan @EnableJms @PropertySource("servicebus.properties") public class Application { @Value("${servicebus.hostname}") private String hostName; @Value("${servicebus.username}") private String userName; @Value("${servicebus.password}") private String password; @Value("${servicebus.queue}") private String queue; /** * * 必须设置,@Value 取值依赖于此 */ @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } /** * * 配置connect factory, Spring也提供多种ConnectionFactory,如:SingleConnectionFactory, * CachingConnectionFactory, 但本例使用Qpid默认提供的ConnectionFactory示例,可以参考Spring的文档, * 查看Spring ConnectionFactoryd的具体用法 */ @Bean ConnectionFactory getConnectionFactory() { ConnectionFactory connectionFactory = new JmsConnectionFactory(userName, password, hostName); return connectionFactory; } /** * * 通常情况下,我们会借助Spring提供的JmsTemplate来实现消息发送 */ @Bean JmsTemplate newJmsTemplate(ConnectionFactory connectionFactory) { return new JmsTemplate(connectionFactory); } /** * * 注册消息消费者和错误监听器 */ @Bean MessageListenerContainer newListenerContainer(ConnectionFactory connectionFactory, Queue queue, MessageConsumerListener messageConsumerListener) { DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer(); messageListenerContainer.setConnectionFactory(connectionFactory); messageListenerContainer.setMessageListener(messageConsumerListener); messageListenerContainer.setDestination(queue); messageListenerContainer.setExceptionListener(new ExceptionListener() { public void onException(JMSException exception) { System.out.println("Connection ExceptionListener fired, exiting."); exception.printStackTrace(System.out); System.exit(1); } }); return messageListenerContainer; } @Bean Queue queue() { return new JmsQueue(queue); } public static void main(String[] args) throws Exception { ApplicationContext context = new AnnotationConfigApplicationContext(Application.class); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); Queue queue = context.getBean(Queue.class); jmsTemplate.send(queue, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage("Hello World"); } }); Thread.sleep(2000); System.exit(1); } }
相关文章推荐
- Android Manifest 用法
- 什么是 GraphQL?
- Spark RDD API详解(一) Map和Reduce
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- lwn拾遗:[sn3218 led drivers]-api解释-1
- 页面元素查找之Selectors API
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 一个小型js框架myJSFrame附API使用帮助
- 详细分析交换机、路由器、集线器的区别和联系
- PowerShell打开或关闭光驱
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页