-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace messageListener with spring integration jms
- Loading branch information
1 parent
9213662
commit 205e3ce
Showing
7 changed files
with
241 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/github/distributionmessage/config/IbmmqFileNameGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.github.distributionmessage.config; | ||
|
||
import org.springframework.messaging.Message; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
|
||
/** | ||
* Created by zhaopei on 17/12/20. | ||
*/ | ||
public class IbmmqFileNameGenerator implements org.springframework.integration.file.FileNameGenerator { | ||
|
||
private String prefix = ""; | ||
|
||
private String suffix = ""; | ||
|
||
private Boolean headerId = false; | ||
|
||
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS"); | ||
|
||
public IbmmqFileNameGenerator(String prefix, String suffix, Boolean headerId) { | ||
this.prefix = prefix; | ||
this.suffix = suffix; | ||
this.headerId = headerId; | ||
} | ||
|
||
public IbmmqFileNameGenerator() { | ||
|
||
} | ||
|
||
@Override | ||
public String generateFileName(Message<?> message) { | ||
StringBuffer buffer = new StringBuffer(this.prefix + "_"); | ||
buffer.append(TIME_FORMAT.format(Calendar.getInstance().getTime())); | ||
if (headerId) { | ||
buffer.append("_" + message.getHeaders().getId()); | ||
} | ||
buffer.append(this.suffix); | ||
return buffer.toString(); | ||
} | ||
|
||
public String getPrefix() { | ||
return prefix; | ||
} | ||
|
||
public void setPrefix(String prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
public String getSuffix() { | ||
return suffix; | ||
} | ||
|
||
public void setSuffix(String suffix) { | ||
this.suffix = suffix; | ||
} | ||
|
||
public Boolean getHeaderId() { | ||
return headerId; | ||
} | ||
|
||
public void setHeaderId(Boolean headerId) { | ||
this.headerId = headerId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/com/github/distributionmessage/constant/ChannelConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.github.distributionmessage.constant; | ||
|
||
public interface ChannelConstant { | ||
|
||
String IBMMQ_RECEIVE_CHANNEL = "ibmmqReceiveChannel"; | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/com/github/distributionmessage/handler/DistributionSendingMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.distributionmessage.handler; | ||
|
||
import com.github.distributionmessage.config.DistributionProp; | ||
import com.github.distributionmessage.constant.CommonConstant; | ||
import com.github.distributionmessage.utils.CommonUtils; | ||
import com.github.distributionmessage.utils.DistributionUtils; | ||
import com.ibm.mq.jms.MQQueue; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import org.springframework.integration.jms.DefaultJmsHeaderMapper; | ||
import org.springframework.integration.jms.JmsHeaderMapper; | ||
import org.springframework.integration.jms.JmsSendingMessageHandler; | ||
import org.springframework.jms.core.JmsTemplate; | ||
import org.springframework.jms.core.MessagePostProcessor; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper=false) | ||
public class DistributionSendingMessageHandler extends JmsSendingMessageHandler { | ||
|
||
private final JmsTemplate jmsTemplate; | ||
|
||
private JmsHeaderMapper headerMapper = new DefaultJmsHeaderMapper(); | ||
|
||
private DistributionProp distributionProp; | ||
|
||
public DistributionSendingMessageHandler(JmsTemplate jmsTemplate) { | ||
super(jmsTemplate); | ||
this.jmsTemplate = jmsTemplate; | ||
} | ||
|
||
@Override | ||
protected void handleMessageInternal(Message<?> message) { | ||
MessagePostProcessor messagePostProcessor = new HeaderMappingMessagePostProcessor(message, this.headerMapper); | ||
Assert.notNull(this.distributionProp, "distributionProp must not be null"); | ||
Assert.notNull(message, "Message must not be null"); | ||
Object playload = message.getPayload(); | ||
Assert.notNull(playload, "Message playload must not be null"); | ||
if (playload instanceof byte[]) { | ||
try { | ||
byte[] bytes = (byte[]) playload; | ||
MQQueue queue = new MQQueue(); | ||
queue.setCCSID(this.distributionProp.getCcsid()); | ||
String sm = new String(bytes, CommonConstant.CHARSET); | ||
String dxpid = DistributionUtils.getDxpIdByMessage(sm); | ||
String msgtype = DistributionUtils.getMessageType(sm); | ||
String queueName = DistributionUtils.getDestinationQueueName(this.distributionProp, dxpid, msgtype); | ||
logger.info("dxpId=[" + dxpid + "] messageType=[" | ||
+ msgtype + "] distributionQueue=[" + queueName + "]"); | ||
queue.setBaseQueueName(queueName); | ||
this.jmsTemplate.convertAndSend(queue, playload, messagePostProcessor); | ||
} catch (Exception e) { | ||
CommonUtils.logError(logger, e); | ||
} | ||
} else { | ||
logger.error("message not is bytes message! message=[" + message + "]"); | ||
} | ||
} | ||
|
||
private static final class HeaderMappingMessagePostProcessor implements MessagePostProcessor { | ||
|
||
private final Message<?> integrationMessage; | ||
|
||
private final JmsHeaderMapper headerMapper; | ||
|
||
HeaderMappingMessagePostProcessor(Message<?> integrationMessage, JmsHeaderMapper headerMapper) { | ||
this.integrationMessage = integrationMessage; | ||
this.headerMapper = headerMapper; | ||
} | ||
|
||
@Override | ||
public javax.jms.Message postProcessMessage(javax.jms.Message jmsMessage) { | ||
this.headerMapper.fromHeaders(this.integrationMessage.getHeaders(), jmsMessage); | ||
return jmsMessage; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters