我正在尝试使用 spring-cloud-starter-aws-messaging
使用Spring Boot项目中来自本地堆栈SQS队列的消息@Configuration
public class SQSConfig {
private final SQSConnectionFactory connectionFactory;
public SQSConfig() {
connectionFactory = SQSConnectionFactory.builder()
.withEndpoint("http://localhost:4576")
.withAWSCredentialsProvider(new StaticCredentialsProvider(
new BasicAWSCredentials("", "")
))
.build();
}
@Bean
public JmsListenerContainerFactory jmsListenerContainerFactory(){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(this.connectionFactory);
factory.setDestinationResolver(new DynamicDestinationResolver());
factory.setConcurrency("3-10");
factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
factory.setMessageConverter(messageConverter());
return factory;
}
@Bean
public MessageConverter messageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setObjectMapper(builder.build());
converter.setTargetType(MessageType.OBJECT);
}
@Service
public class QueueService {
@JmsListener(destination = "myqueuename")
public String listen(Object message){
System.out.println(message);
return null;
}
}
系统显示我正在接收的对象是SQSTextMessage
com.amazon.sqs.javamessaging.message.SQSTextMessage@8390af8
所以我可以这样做:
@JmsListener(destination = "e-legajo")
public String listen(SQSTextMessage message){
try {
String message1 = message.getText();
} catch (JMSException e) {
e.printStackTrace();
}
System.out.println(message);
return null;
}
并处理String消息。
但是,这是我的疑问。我看到亚马逊还提供了其他类型的消息,例如SQSObjectMessage和SQSBytesMessages,我试图找出如何使用SQSObjectMessage的方法,这样我可以直接接收Java对象,但找不到路。
我没有找到此实现的任何示例,因为这是我第一次使用一般队列(SQS或JMS),所以我不知道对此负责的人是JMSListener,MessageConverter还是其他任何人其他在中间发挥神奇作用的AWS JDK Bean?