带注释的路由器提供byte []作为参数而不是POJO

时间:2015-04-06 19:05:58

标签: spring-integration

我正在使用

Spring Integration 4.1.2.RELEASE

我正在尝试通过注释配置有效负载路由器。但是,我收到一个byte []作为方法参数而不是消息POJO。我尝试按照SO answer中的建议对我的解决方案进行建模,但是答案使用本机类型作为消息,而我希望使用POJO。我在下面的配置代码中的auditPayloadRouter配置中做错了什么?

有问题的注释:

    @Router(inputChannel="auditRoutingChannel", autoStartup="true",
        channelMappings= 
         {"com.company.data.model.shared.AccountAudit=audit.account",
          "com.company.data.model.shared.BlacklistAudit=audit.blacklist",
          "com.company.data.model.shared.MediaAudit=audit.item"})
    public String  auditPayloadRouter(Object o)  

完整配置:

@Configuration
@EnableIntegration
public class AuditConfig2 {

    @Autowired
    AuditMessagingOperations auditMessagingOperations;

    private Queue auditQueue;
    private ConnectionFactory connectionFactory;

    @Autowired
    public void setAuditQueue(Queue auditQueue) {
        this.auditQueue = auditQueue;
    }

    @Autowired
    public void setConnectionFactory (ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    @Bean
    public AmqpInboundChannelAdapter amqpInboundChannelAdapter(
        @Qualifier("jsonMessageConverter") MessageConverter messageConverter
        , MessageChannel errorChannel) {

        SimpleMessageListenerContainer listenerContainer =
            new SimpleMessageListenerContainer(connectionFactory);
        listenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        listenerContainer.setAutoStartup(true);
        listenerContainer.setConcurrentConsumers(1);
        listenerContainer.setMessageConverter(messageConverter);
        listenerContainer.setQueues(auditQueue);
        listenerContainer.setChannelTransacted(false);
        listenerContainer.setPrefetchCount(100);

        AmqpInboundChannelAdapter a = new
            AmqpInboundChannelAdapter(listenerContainer);
        a.setAutoStartup(true);
        a.setErrorChannel(errorChannel);
        a.setOutputChannel(auditRoutingChannel());
        return a;
    }

    @Bean(name="auditRoutingChannel")
    public MessageChannel auditRoutingChannel() {
        DirectChannel c = new DirectChannel();
        c.setComponentName("auditRoutingChannel");
        c.setBeanName("auditRoutingChannel");
        return c;
    }

    @Router(inputChannel="auditRoutingChannel", autoStartup="true",
        channelMappings= 
         {"com.company.data.model.shared.AccountAudit=audit.account",
          "com.company.data.model.shared.BlacklistAudit=audit.blacklist",
          "com.company.data.model.shared.MediaAudit=audit.item"})
    public String  auditPayloadRouter(Object o) {
        if (o == null) {
            return "errorChannel";
        }
        //DEBUG - Print out the unexpected byte[]
        String out = null;
        try {
            out = new String((byte[])o, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
        System.out.println(out);
        if (o instanceof AccountAudit ||
            o instanceof BlacklistAudit ||
            o instanceof MediaAudit) {

            return o.getClass().getName();
        }
        return "nullChannel";
    }
}

1 个答案:

答案 0 :(得分:0)

如果content_type邮件属性包含json,则JSON邮件转换器只会转换json,例如application/json

否则,有效负载仍为byte[]

在这种情况下,您会看到警告日志。

log.warn("Could not convert incoming message with content-type ["
    + contentType + "]");