1
votes

Have an incoming message from ActiveMQ queue and the message is being delivered properly. I need to access the JMS header value x-cutoffrule in my spring integration flow, but the value of cutoffrule in the handle section always is coming as null. My code is below:

@Bean
public JmsHeaderMapper sampleJmsHeaderMapper() {
    return new DefaultJmsHeaderMapper() {
        public Map<String, Object> toHeaders(javax.jms.Message jmsMessage) {
            Map<String, Object> headers = super.toHeaders(jmsMessage);
            try {
                headers.put("cutoffrule", jmsMessage.getStringProperty("x-cutoffrule"));
            } catch (JMSException e) {
                e.printStackTrace();
            }
            return headers;
        }
    };
}


@Bean
public IntegrationFlow jmsMessageDrivenFlow(JmsHeaderMapper sampleJmsHeaderMapper ) {
    return IntegrationFlows
            .from(
                    Jms.messageDriverChannelAdapter(jmsMessagingTemplate.getConnectionFactory())
                            .destination(integrationProps.getIncomingRequestQueue())
                            .errorChannel(errorChannel())
                            .setHeaderMapper( sampleJmsHeaderMapper )
            )
            .handle((payload, headers) -> {
                incomingPayload = payload;
                logger.debug("cutoffrule"+ headers.get("cutoffrule"));
                return payload;
            })
            .handle(message -> {
                logger.debug("Message was succcessfully processed");
            })
            .get();
}

I thought the DefaultJmsHeaderMapper will map all JMS headers into the spring integration message. What am I missing?

1
Why do you use sampleJmsHeaderMapper? Why don't use "x-cutoffrule" inside your (payload, headers) -> {...} handler?Andriy Kryvtsun
thats how i started by putting it directly in the handle(payload, headers), but I wasn't getting the values. So, moved to make it explicit by using DefaultJmsHeaderMapper with this implementation. Still doesn't work.StartingDev
I wonder if your message really has that x-cutoffrule property... Please, debug your for confirmation. When we found the property, you will be able to map it into the appropriate header from your custom toHeaders()Artem Bilan
I would try to remove JmsHeaderMapper, extract your handler as a separate class as a successor of AbstractMessageProcessingTransformer and change call from handle to transform in your chain. And in your successed transform method check presents of your field in Message<?> message parameter.Andriy Kryvtsun
@StartingDev as you can see in my prev. comment message param has org.springframework.messaging.Message type with getPayload and getHeaders methods.Andriy Kryvtsun

1 Answers

1
votes

The best way to understand what's wrong it to debug the code. Or, at least log everything. The best place for you is that your DefaultJmsHeaderMapper extension.

So, the DefaultJmsHeaderMapper maps all incoming properties. But it does that with the getObjectProperty() not getStringProperty(), like in your code:

Enumeration<?> jmsPropertyNames = jmsMessage.getPropertyNames();
if (jmsPropertyNames != null) {
    while (jmsPropertyNames.hasMoreElements()) {
        String propertyName = jmsPropertyNames.nextElement().toString();
        try {
            String headerName = this.toHeaderName(propertyName);
            headers.put(headerName, jmsMessage.getObjectProperty(propertyName));
        }
        catch (Exception e) {
            if (logger.isWarnEnabled()) {
                logger.warn("error occurred while mapping JMS property '"
                            + propertyName + "' to Message header", e);
            }
        }
    }
}

So, your x-cutoffrule should be mapped exactly into the x-cutoffrule header.

See Andriy's comment, too.