Spring boot 使用Spring集成一次发送单个json对象字符串和消息负载?
我使用spring integration使用JdbcPollingChannelAdapter轮询数据库,然后使用JmsSendingMessageHandler将结果发布到Activemq队列。我正在使用MappingJackson2MessageConverter将jdbc结果序列化为json字符串。发送消息get时,它将作为arraylist发送。一次只能发送一个带有消息负载的json序列化对象吗?这样我就可以像这样监听队列了Spring boot 使用Spring集成一次发送单个json对象字符串和消息负载?,spring-boot,jms,spring-integration,activemq,Spring Boot,Jms,Spring Integration,Activemq,我使用spring integration使用JdbcPollingChannelAdapter轮询数据库,然后使用JmsSendingMessageHandler将结果发布到Activemq队列。我正在使用MappingJackson2MessageConverter将jdbc结果序列化为json字符串。发送消息get时,它将作为arraylist发送。一次只能发送一个带有消息负载的json序列化对象吗?这样我就可以像这样监听队列了 @JmsListener(destination = "${
@JmsListener(destination = "${activemq.queue.name}")
public void receive(DomainObj obj)
Spring集成配置
@Configuration
public class SpringIntegrationConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringIntegrationConfig.class);
@Value("${database.polling-interval.rate-in-milliseconds}")
private Long pollingRateInMilliSeconds;
@Value("${database.max-messages-per-poll}")
private Long maxMessagesPerPoll;
@Bean
public MessageChannel helloWorldChannel() {
return new DirectChannel();
}
@Bean
public PollerMetadata poller(PlatformTransactionManager transactionManager) {
PeriodicTrigger trigger = new PeriodicTrigger(pollingRateInMilliSeconds);
trigger.setFixedRate(true);
MatchAlwaysTransactionAttributeSource attributeSource = new MatchAlwaysTransactionAttributeSource();
attributeSource.setTransactionAttribute(new DefaultTransactionAttribute());
TransactionInterceptor interceptor = new TransactionInterceptor(transactionManager, attributeSource);
PollerMetadata poller = new PollerMetadata();
poller.setTrigger(trigger);
poller.setMaxMessagesPerPoll(maxMessagesPerPoll);
poller.setAdviceChain(Collections.singletonList(interceptor));
return poller;
}
@Bean
@InboundChannelAdapter(value = "helloWorldChannel", channel = "helloWorldChannel", poller = @Poller("poller"))
public MessageSource<?> helloWorldMessageSource(DataSource dataSource) {
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(dataSource, "select * from item where type = 2");
adapter.setUpdateSql("update item set type = 10 where id in (:id)");
adapter.setRowMapper(new ItemRowMapper());
adapter.setMaxRowsPerPoll(maxMessagesPerPoll.intValue());
return adapter;
}
@Bean
@ServiceActivator(inputChannel = "helloWorldChannel")
public MessageHandler jsmOutboundAdapter(JmsTemplate template, Queue queue, MessageConverter converter) {
template.setMessageConverter(converter);
JmsSendingMessageHandler handler = new JmsSendingMessageHandler(template);
handler.setDestination(queue);
return handler;
}
@Bean // Serialize message content to json using TextMessage
public MessageConverter jsonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
}
@配置
公共类SpringIntegrationConfig{
私有静态最终记录器Logger=LoggerFactory.getLogger(SpringIntegrationConfig.class);
@值(${database.polling interval.rate(毫秒)}”)
私有长轮询数毫秒;
@值(${database.max messages per poll}”)
专用长maxMessagesPerPoll;
@豆子
公共消息频道helloWorldChannel(){
返回新的DirectChannel();
}
@豆子
公共轮询器数据轮询器(平台事务管理器事务管理器){
PeriodicTrigger触发器=新的PeriodicTrigger(轮询时间毫秒);
trigger.setFixedRate(true);
MatchAlwaysTransactionAttributeSource attributeSource=新的MatchAlwaysTransactionAttributeSource();
attributeSource.SetTransactionaAttribute(新的DefaultTransactionaAttribute());
TransactionInterceptor interceptor=新的TransactionInterceptor(transactionManager,attributeSource);
PollerMetadata poller=新PollerMetadata();
poller.setTrigger(触发器);
poller.setMaxMessagesPerPoll(maxMessagesPerPoll);
poller.setAdviceChain(Collections.singletonList(拦截器));
回程轮询器;
}
@豆子
@InboundChannelAdapter(value=“helloWorldChannel”,channel=“helloWorldChannel”,poller=@poller(“poller”))
公共消息源helloWorldMessageSource(数据源数据源){
JdbcPollingChannelAdapter=新的JdbcPollingChannelAdapter(数据源,“从类型为2的项中选择*);
setUpdateSql(“updateitemsettype=10,其中id在(:id)”中);
setRowMapper(新的ItemRowMapper());
setMaxRowsPerPoll(maxMessagesPerPoll.intValue());
返回适配器;
}
@豆子
@ServiceActivator(inputChannel=“helloWorldChannel”)
public MessageHandler jsmOutboundAdapter(JmsTemplate模板、队列、MessageConverter转换器){
模板.setMessageConverter(转换器);
JmsSendingMessageHandler=新的JmsSendingMessageHandler(模板);
setDestination(队列);
返回处理程序;
}
@Bean//使用TextMessage将消息内容序列化为json
公共消息转换器jsonJmsMessageConverter(){
MappingJackson2MessageConverter=新的MappingJackson2MessageConverter();
setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName(“_-type”);
回流转换器;
}
}
更改select语句以使用JDBC供应商语法仅检索一条记录-例如限制1
然后,删除
setMaxRowsPerPoll()
(将其保留为默认值0),您将获得单个结果。@Transformer公共对象转换(列表列表){return List.get(0);}
使用SQL限制。这就是让它返回json对象而不是数组的方法 谢谢。我真的很感谢你的帮助,我只是试了一下。虽然它只得到一个结果,但对象仍然被序列化为json数组。有可能将结果序列化为json对象吗;它总是一张清单;您可以将@Transformer
添加到流公共对象转换(列表){return List.get(0);}
。啊,好的。这很有效。非常感谢你。我感谢你的帮助。