Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.bridge.jms.integration; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.jms.JMSException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.integration.MessageHeaders; import org.springframework.integration.jms.DefaultJmsHeaderMapper; import org.springframework.util.StringUtils; /** * Group ID JMS header mapper. * * @author Jasper van Veghel <jasper@seajas.com> */ public class GroupIdJmsHeaderMapper extends DefaultJmsHeaderMapper { /** * The logger. */ private static final Logger logger = LoggerFactory.getLogger(GroupIdJmsHeaderMapper.class); /** * Constants. */ private static final String JMS_HEADER_GROUPID = "JMSXGroupID"; private static final String SPI_HEADER_GROUPID = "queue.channel.group"; private static final String JMS_HEADER_SEAJAS_DOMAIN = "SeajasDomain"; private static final String SPI_HEADER_SEAJAS_DOMAIN = "queue.channel.domain"; /** * Whether to use message groups. */ @Value("${bridged.project.jms.use.message.groups}") private Boolean useMessageGroups; /** * The exclusions. */ private List<String> exclusions; /** * Set the exclusions. * * @param exclusions */ @Value("${bridged.project.retrieval.excluded.hosts}") public void setExclusions(final String exclusions) { this.exclusions = Arrays.asList(StringUtils.tokenizeToStringArray(exclusions, ",", true, true)); } /** * Default constructor. */ public GroupIdJmsHeaderMapper() { super(); } @Override public void fromHeaders(final MessageHeaders headers, final javax.jms.Message jmsMessage) { Object groupIdHeader = headers.get(SPI_HEADER_GROUPID); String groupId = groupIdHeader != null && groupIdHeader instanceof String ? (String) groupIdHeader : null; if (groupIdHeader != null && !(groupIdHeader instanceof String)) logger.warn("A group ID header (" + SPI_HEADER_GROUPID + ") was set, but its type is invalid - should be String - not adding the message to a group"); super.fromHeaders(headers, jmsMessage); if (useMessageGroups && groupId != null && !exclusions.contains(groupId)) try { jmsMessage.setStringProperty(JMS_HEADER_GROUPID, groupId); } catch (JMSException e) { logger.error("Could not set the JMS " + JMS_HEADER_GROUPID + " header", e); } try { jmsMessage.setStringProperty(JMS_HEADER_SEAJAS_DOMAIN, groupId); } catch (JMSException e) { logger.error("Could not set the JMS " + JMS_HEADER_SEAJAS_DOMAIN + " header", e); } } @Override public Map<String, Object> toHeaders(final javax.jms.Message jmsMessage) { Map<String, Object> result = super.toHeaders(jmsMessage); try { if (jmsMessage.getStringProperty(JMS_HEADER_GROUPID) != null) result.put(SPI_HEADER_GROUPID, jmsMessage.getStringProperty(JMS_HEADER_GROUPID)); if (jmsMessage.getStringProperty(JMS_HEADER_SEAJAS_DOMAIN) != null) { result.put(SPI_HEADER_SEAJAS_DOMAIN, jmsMessage.getStringProperty(JMS_HEADER_SEAJAS_DOMAIN)); } } catch (JMSException e) { logger.error("Could not retrieve the JMS " + JMS_HEADER_GROUPID + " or " + JMS_HEADER_SEAJAS_DOMAIN + " header", e); } return result; } }