当前位置: 首页 > 工具软件 > Sqs4J > 使用案例 >

aws sqs_在Spring使用AWS SQS创建消息驱动Bean

岳刚洁
2023-12-01

aws sqs

我之前的文章中,我展示了一个简单的示例,该示例如何将AWS SQSSpring Framework结合使用以将消息放入队列并从队列中读取消息。 在本文中,我将更进一步,并使用Spring创建一个“消息驱动的Bean”,以便对放入队列中的每个消息都进行“自动”处理。 AWS在其文档页面上将称为异步方式。 要做到这一点,我将在Spring中定义一个MessageListener并将其配置为描述来听我的队列在这里 。 要查看初始项目设置,请参阅我的前一篇文章,因为在此不再赘述。

Spring应用程序上下文将定义消息侦听器(和相应的对象),如下所示:

<bean id="amazonMessageListener" class="net.pascalalma.aws.sqs.SpringMessageDrivenBean" />

  <bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
    <property name="delegate" ref="amazonMessageListener"/>
    <property name="defaultListenerMethod" value="onMessage"/>
    <property name="messageConverter">
      <null/>
    </property>
  </bean>

  <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="destinationName" ref="queueName" />
    <property name="messageListener" ref="messageListener" />
  </bean>

首先,我定义了我的MDB(MessageDrivenBean)bean,并将其称为“ amazonMessageListener”。 接下来,我将此MDB用作“ messageListener”适配器的“代理”。 这个“适配器” bean还可以负责转换消息有效负载(在此忽略)并在委托的侦听器中调用正确的方法。

在'jmsContainer'bean中,'adapter'与使用的JMS连接Factory和目标链接在一起。

剩下的就是MDB本身的源代码:

package net.pascalalma.aws.sqs;

import org.apache.log4j.Logger;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

public class SpringMessageDrivenBean {

    final static Logger logger = Logger.getLogger(SpringMessageDrivenBean.class);

    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                logger.info(String.format("MDB received: %s ", ((TextMessage) message).getText()));
            }
            catch (JMSException ex) {
                throw new RuntimeException(ex);
            }
        }
        else {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }
}

我认为这非常简单。 对于每个放入队列的消息,都会调用方法“ onMessage”,在这种情况下,它仅打印消息的文本内容。 为了查看它的工作原理,我使用以下“主”类:

package net.pascalalma.aws.sqs;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMdbMain {

    public static void main(String[] args) {
        //Build application context by reading spring-config.xml
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"application-context.xml"});

        //Get an instance of ProviderService class;
        MyMessageProvider prdSvc = (MyMessageProvider) ctx.getBean("myMessageProviderService");

        //Call getProduct method of ProductService
        prdSvc.sendMessage("This is a test A");
        prdSvc.sendMessage("This is a test B");
        prdSvc.sendMessage("This is a test C");
        prdSvc.sendMessage("This is a test D");
    }
}

结果为以下输出:

2015-04-11 13:17:20 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test A
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test B
2015-04-11 13:17:26 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test A 
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test C
2015-04-11 13:17:26 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test B 
2015-04-11 13:17:27 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:27 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test D
2015-04-11 13:17:27 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test C 
2015-04-11 13:17:27 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:27 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test D

翻译自: https://www.javacodegeeks.com/2015/05/creating-a-message-driven-bean-with-aws-sqs-in-spring.html

aws sqs

 类似资料: