aws sqs

Amazon WEB服务为我们提供了SQS消息传递服务。 sqs的java sdk与JMS兼容。

因此,可以将SQS与spring提供的JMS集成框架集成在一起,而不是将SQS用作简单的spring bean。

我将使用spring-boot和gradle。

gradle文件:

group 'com.gkatzioura.sqstesting'
version '1.0-SNAPSHOT'buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")}
}apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'sourceCompatibility = 1.8repositories {mavenCentral()
}dependencies {compile "org.springframework.boot:spring-boot-starter-thymeleaf"compile "com.amazonaws:aws-java-sdk:1.10.55"compile "org.springframework:spring-jms"compile "com.amazonaws:amazon-sqs-java-messaging-lib:1.0.0"compile 'org.slf4j:slf4j-api:1.6.6'compile 'ch.qos.logback:logback-classic:1.0.13'testCompile "junit:junit:4.11"
}

应用类别

package com.gkatzioura.sqstesting;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Created by gkatziourasemmanouil on 8/26/15.*/
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

并应用yml文件

  • 队列:
  • 端点: http:// localhost:9324
  • 名称:样本队列

我指定了一个本地主机端点,因为我使用了ElasticMq 。

SQSConfig类是一个配置类,以使SQS客户端作为spring bean可用。

package com.gkatzioura.sqstesting.config;import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Created by gkatziourasemmanouil on 25/02/16.*/
@Configuration
public class SQSConfig {@Value("${queue.endpoint}")private String endpoint;@Value("${queue.name}")private String queueName;@Beanpublic AmazonSQSClient createSQSClient() {AmazonSQSClient amazonSQSClient = new AmazonSQSClient(new BasicAWSCredentials("",""));amazonSQSClient.setEndpoint(endpoint);amazonSQSClient.createQueue(queueName);return amazonSQSClient;}}

SQSListener是实现JMS MessageListener接口的侦听器类。

package com.gkatzioura.sqstesting.listeners;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;/*** Created by gkatziourasemmanouil on 25/02/16.*/
@Component
public class SQSListener implements MessageListener {private static final Logger LOGGER = LoggerFactory.getLogger(SQSListener.class);public void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {LOGGER.info("Received message "+ textMessage.getText());} catch (JMSException e) {LOGGER.error("Error processing message ",e);}}
}

JMSSQSConfig类包含JmsTemplate和DefaultMessageListenerContainer的配置。 通过JMSSQSConfig类,我们注册了JMS MessageListeners。

package com.gkatzioura.sqstesting.config;import com.amazon.sqs.javamessaging.SQSConnectionFactory;
import com.amazonaws.auth.*;
import com.gkatzioura.sqstesting.listeners.SQSListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;/*** Created by gkatziourasemmanouil on 25/02/16.*/
@Configuration
public class JMSSQSConfig {@Value("${queue.endpoint}")private String endpoint;@Value("${queue.name}")private String queueName;@Autowiredprivate SQSListener sqsListener;@Beanpublic DefaultMessageListenerContainer jmsListenerContainer() {SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder().withAWSCredentialsProvider(new DefaultAWSCredentialsProviderChain()).withEndpoint(endpoint).withAWSCredentialsProvider(awsCredentialsProvider).withNumberOfMessagesToPrefetch(10).build();DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();dmlc.setConnectionFactory(sqsConnectionFactory);dmlc.setDestinationName(queueName);dmlc.setMessageListener(sqsListener);return dmlc;}@Beanpublic JmsTemplate createJMSTemplate() {SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder().withAWSCredentialsProvider(awsCredentialsProvider).withEndpoint(endpoint).withNumberOfMessagesToPrefetch(10).build();JmsTemplate jmsTemplate = new JmsTemplate(sqsConnectionFactory);jmsTemplate.setDefaultDestinationName(queueName);jmsTemplate.setDeliveryPersistent(false);return jmsTemplate;}private final AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {@Overridepublic AWSCredentials getCredentials() {return new BasicAWSCredentials("", "");}@Overridepublic void refresh() {}};}

MessageService是使用JMSTemplate以便将消息发送到队列的服务

package com.gkatzioura.sqstesting;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;/*** Created by gkatziourasemmanouil on 28/02/16.*/
@Service
public class MessageService {@Autowiredprivate JmsTemplate jmsTemplate;@Value("${queue.name}")private String queueName;private static final Logger LOGGER = LoggerFactory.getLogger(MessageService.class);public void sendMessage(final String message) {jmsTemplate.send(queueName, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage(message);}});}}

最后但并非最不重要的一点是添加了控制器。 控制器将发布请求正文作为消息发送到队列。

package com.gkatzioura.sqstesting;import com.amazonaws.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;/*** Created by gkatziourasemmanouil on 24/02/16.*/
@Controller
@RequestMapping("/main")
public class MainController {@Autowiredprivate MessageService messageService;@RequestMapping(value = "/write",method = RequestMethod.POST)public void write(HttpServletRequest servletRequest,HttpServletResponse servletResponse) throws IOException {InputStream inputStream = servletRequest.getInputStream();String message = IOUtils.toString(inputStream);messageService.sendMessage(message);}}
  • 您可以在此处下载源代码。

翻译自: https://www.javacodegeeks.com/2016/02/aws-sqs-spring-jms-integration.html

aws sqs

aws sqs_AWS SQS和Spring JMS集成相关推荐

  1. AWS SQS和Spring JMS集成

    Amazon WEB服务为我们提供了SQS消息传递服务. sqs的java sdk与JMS兼容. 因此,可以将SQS与spring提供的JMS集成框架集成在一起,而不是将SQS用作简单的spring ...

  2. Thymeleaf MVC与Spring的集成

    源码下载链接: 待集成的原项目 样例 **本篇博客主要是在上述两个工程的基础上,仿照stsm工程为gtvg集成spring框架,使其@Controller等元数据,通过对 Thymeleaf +Spr ...

  3. Spring Boot集成Swagger导入YApi@无界编程

    接口APi开发现状 现在开发接口都要在类似YApi上写文档,这样方便不同的团队之间协作,同步更新接口,提高效率. 但是如果接口很多,你一个个手工在YApi去录入无疑效率很低. 如果是使用Spring ...

  4. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例...

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  5. Spring MVC集成slf4j-logback - 我想跟代码谈谈 - 博客频道 - CSDN.NET

    Spring MVC集成slf4j-logback - 我想跟代码谈谈 - 博客频道 - CSDN.NET

  6. spring cloud微服务分布式云架构 - Spring Cloud集成项目简介

    Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...

  7. 6.3 Spring Boot集成mongodb开发

    6.3 Spring Boot集成mongodb开发 本章我们通过SpringBoot集成mongodb,Java,Kotlin开发一个极简社区文章博客系统. 0 mongodb简介 Mongo 的主 ...

  8. springboot(十八):使用Spring Boot集成FastDFS

    上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Spring Boot将文件上传到分布式文件系统FastDFS中. 这个项目会在上一个项目的基础上进行构建. 1.pom包 ...

  9. Spring JMS

    异步处理通信是面向服务架构(SOA)的重要部分,因为企业中的许多系统通信,尤其是跟外部系统通信本来就是异步的.Java消息服务(JMS)就是用来编写异步消息J2EE应用的API.使用JMS API的传 ...

最新文章

  1. 这或许是东半球分析十大排序算法最好的一篇文章
  2. 20-Granule Protection Tables Library
  3. springboot spring.config.location外挂配置文件实战
  4. 【ArcGIS遇上Python】ArcGIS Python获取某个字段的唯一值(获取指定字段的不同属性值)
  5. Kotlin学习笔记26 协程part6 协程与线程的关系 Dispatchers.Unconfined 协程调试 协程上下文切换 Job详解 父子协程的关系
  6. ASP.NET MVC实践系列11-FCKEditor和CKEditor的使用
  7. Castle的自定义类型转换
  8. webpack之loader篇
  9. 计算机论文中期报告进展情况,自动化毕业论文中期报告进展情况怎么写
  10. css中标准盒模型和怪异盒模型的区别,如何将标准盒模型转换为怪异盒模型
  11. VirtualBox+CentOS6.5安装增强功能包 - Building the main Guest Additions module [失败]
  12. 考研调剂 计算机科学 软件,四川大学计算机学院(软件学院)2020非全日制考研调剂信息...
  13. CSS中的text-overflow属性详解 (控制文字在一行显示,超出部分加省略号)
  14. 安卓10 来电流程梳理
  15. 主平台对接多个系统,系统表的性能和对接方案
  16. oracle 强制还原一张表,oracle数据库中,用户不小心在生产环境中删除了一张比较重要的表,他想恢复该操作,你会采取什么样的...
  17. 虚拟主机的配置以及解决访问不了localhost和127.0.0.1
  18. Home Server
  19. 库卡机器人配重_干货 | 库卡机器人使用教程
  20. controls提供添加播放、暂停、和音量的控件

热门文章

  1. CF755G-PolandBall and Many Other Balls【倍增FFT】
  2. P3329-[ZJOI2011]最小割【最小割树】
  3. YbtOJ#20060-[NOIP2020模拟赛B组Day3]字串修改【模拟】
  4. 【期望】【高斯消元】图上游走(金牌导航 期望-6)
  5. 【二分】买礼物的艰辛
  6. 动态规划训练9 [Brackets POJ - 2955 ]
  7. MongoDB新建或删除索引
  8. 深入并发包-ConcurrentHashMap
  9. hibernate框架之主键生成
  10. Ajax传递json数据