配置入站消息传递:
消息传递配置在jboss.xml文件中,这个文件的内容会有所不同,但大致如下:
jboss.xml
<jboss xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss_5_0.xsd" version="5.0">
<enterprise-beans>
<message-driven>
<ejb-name>MyMessageBean</ejb-name>
<!-- Make sure following System properties are defined in JBoss before deploying the application -->
<activation-config>
<activation-config-property>
<activation-config-property-name>DestinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>MY.APP.INBOUND.QUEUE</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>channel</activation-config-property-name>
<activation-config-property-value>${channel}</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>hostName</activation-config-property-name>
<activation-config-property-value>${hostName}</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>port</activation-config-property-name>
<activation-config-property-value>1414</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>queueManager</activation-config-property-name>
<activation-config-property-value>${queueManager}</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>transportType</activation-config-property-name>
<activation-config-property-value>CLIENT</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>username</activation-config-property-name>
<activation-config-property-value>munish</activation-config-property-value>
</activation-config-property>
</activation-config>
<!-- instruct the MDB to use the WebSphere MQ resource adapter -->
<resource-adapter-name>wmq.jmsra.rar</resource-adapter-name>
</message-driven>
</enterprise-beans>
</jboss>
必须指定目标类型和名称属性。其他属性都是可选的,如果省略,将使用其默认值。
一个简单的MDB定义入站消息如下所示:
/**
* This message driven bean is used to collect asynchronously received messages.
*
* <A class=referer href="http://www.linuxidc.com" target=_blank>@author</A> Munish Gogna
*/
@MessageDriven(mappedName = "jms/IncomingQueue")
public final class MyMessageBean implements javax.jms.MessageListener {
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MyMessageBean.class);
public void onMessage(final javax.jms.Message message) {
LOGGER.info("Received message: " + message);
// TODO - do processing with the message here
}
}
一个简单的java类发送消息到输出队列如下所示:
/**
* This class is used to send text messages to a queue.
*
* <A class=referer href="http://www.linuxidc.com" target=_blank>@author</A> Munish Gogna
*/
public final class MessageSender {
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MessageSender.class);
/**
* A coarse-grained method, nothing fancy. In actual applications please re factor the code and
* handle the exceptions properly.
*
* @throws Exception
*/
public void send(final String message) throws Exception {
if (message == null) {
throw new IllegalArgumentException("Parameter message cannot be null");
}
javax.jms.QueueConnection connection = null;
javax.jms.Session session = null;
try {
javax.naming.Context fContext = new javax.naming.InitialContext();
javax.jms.QueueConnectionFactory fConnectionFactory = (javax.jms.QueueConnectionFactory) fContext
.lookup("java:jms/MyAppConnectionFactory");
connection = fConnectionFactory.createQueueConnection();
session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
final javax.naming.Context jndiContext = new javax.naming.InitialContext();
final javax.jms.Destination destination = (javax.jms.Destination) jndiContext.lookup("jms/OutgoingQueue");
final javax.jms.MessageProducer messageProducer = session.createProducer(destination);
final javax.jms.TextMessage textMessage = session.createTextMessage(message);
messageProducer.send(textMessage);
messageProducer.close();
} catch (Exception nex) {
throw nex;
} finally {
if (session != null) {
try {
session.close();
} catch (JMSException e) {
LOGGER.error("Failed to close JMS session", e);
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
LOGGER.error("Failed to close JMS connection", e);
}
}
}
}
}
最终RFHUTILC.EXE可用于连接到远程队列。
