我希望我的出站jms消息(队列或主题)包含某些出站属性。是否可以在一个地方说明(例如,在连接器级别)?
答案 0 :(得分:3)
为此,你必须:
org.mule.transport.jms.transformers.ObjectToJMSMessage
并使其为所创建的JMS消息添加所需的属性,将此自定义转换器注册为已考虑的JMS连接器的新默认出站转换器:
<jms:connector name="jmsConnector">
<service-overrides outboundTransformer="apackage.IpAddingObjectToJMSMessage"/>
</jms:connector>
以下是自定义转换器的代码:
package apackage;
import java.net.InetAddress;
import javax.jms.Message;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.config.i18n.MessageFactory;
import org.mule.transport.jms.transformers.ObjectToJMSMessage;
public class IpAddingObjectToJMSMessage extends ObjectToJMSMessage
{
@Override
public Object transformMessage(final MuleMessage message, final String outputEncoding)
throws TransformerException
{
final Message jmsMessage = (Message) super.transformMessage(message, outputEncoding);
try
{
jmsMessage.setStringProperty("IP", InetAddress.getLocalHost().getHostAddress());
}
catch (final Exception e)
{
throw new TransformerException(
MessageFactory.createStaticMessage("Failed to add IP address to JMS message"), e);
}
return jmsMessage;
}
}