日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

aws sqs_AWS SQS和Spring JMS集成

發布時間:2023/12/3 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aws sqs_AWS SQS和Spring JMS集成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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集成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。