进行编程以直接使用 JMS 和消息传递
企业应用程序可以直接使用 Java™ 消息服务 (JMS) 编程接口来提供消息传递服务以及实现业务逻辑的方法。
关于此任务
WebSphere® Application Server 支持将异步消息传递作为基于 JMS 编程接口的通信方法。 通过 JMS,企业应用程序可以使用 JMS 目标(队列或主题)与其他 JMS 客户机异步交换消息。企业应用程序可以显式轮询目标上的消息。
如果您选择不使用 JNDI 来获取消息传递提供程序(例如连接工厂或目标)的配置信息,那么可以改为使用消息传递提供程序所提供的 API 来以编程方式指定该配置信息。
如果要在 JMS 应用程序与传统的 IBM MQ 应用程序之间传送消息,那么必须考虑如何将 JMS 消息结构映射至 IBM MQ 消息。这包括您要使用 IBM MQ 处理在两个 JMS 应用程序之间传送消息的方案;例如,通过将 IBM MQ 用作消息代理。
缺省情况下,IBM MQ 队列上保留的 JMS 消息使用 MQRFH2 头保留一些 JMS 消息头信息。许多传统的 IBM MQ 应用程序无法处理带有这些头的消息,并且需要自身特有的头,例如用于 IBM MQ 工作流程应用程序的 MQWIH。有关如何将 JMS 消息结构映射至 IBM MQ 消息的更多信息,请参阅 IBM MQ 信息中心中的映射 JMS 消息一节。
过程
示例
以下示例显示如何通过程序为缺省消息传递提供程序配置资源。
在此示例中,与服务集成总线的 JMS 连接是使用 com.ibm.websphere.sib 包中的 API 创建的。这是使用 JNDI 来查询通过管理方式配置的连接工厂的备用方法。建立连接后,该样本程序会从控制台读取输入行并将它们作为 JMS 文本消息发送至指定目标。
此示例可作为瘦客户机应用程序或独立客户机应用程序运行。
/*
* Sample program
* © COPYRIGHT International Business Machines Corp. 2009
* All Rights Reserved * Licensed Materials - Property of IBM
*
* This sample program is provided AS IS and may be used, executed,
* copied and modified without royalty payment by customer
*
* (a) for its own instruction and study,
* (b) in order to develop applications designed to run with an IBM
* WebSphere product for the customer's own internal use.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.websphere.sib.api.jms.JmsConnectionFactory;
import com.ibm.websphere.sib.api.jms.JmsFactoryFactory;
import com.ibm.websphere.sib.api.jms.JmsQueue;
import com.ibm.websphere.sib.api.jms.JmsTopic;
/**
* Sample code to programmatically create a connection to a bus and
* send a text message.
*
* Example command lines:
* SIBusSender topic://my/topic?topicSpace=Default.Topic.Space MyBus localhost:7276
* SIBusSender queue://myQueue MyBus localhost:7286:BootstrapSecureMessaging InboundSecureMessaging
*/
public class SIBusSender {
/**
* @param args DEST_URL,BUS_NAME,PROVIDER_ENDPOINTS,[TRANSPORT_CHAIN]
*/
public static void main(String[] args) throws JMSException, IOException {
// Parse the arguments
if (args.length < 3) {
throw new IllegalArgumentException(
"Usage: SIBusSender <DEST_URL> <BUS_NAME> <PROVIDER_ENDPOINTS> [TARGET_TRANSPORT_CHAIN]");
}
String destUrl = args[0];
String busName = args[1];
String providerEndpoints = args[2];
String targetTransportChain = "InboundBasicMessaging";
if (args.length >= 4) targetTransportChain = args[3];
// Obtain the factory factory
JmsFactoryFactory jmsFact = JmsFactoryFactory.getInstance();
// Create a JMS destination
Destination dest;
if (destUrl.startsWith("topic://")) {
JmsTopic topic = jmsFact.createTopic(destUrl);
// Setter methods could be called here to configure the topic
dest = topic ;
}
else {
JmsQueue queue = jmsFact.createQueue(destUrl);
// Setter methods could be called here to configure the queue
dest = queue;
}
// Create a unified JMS connection factory
JmsConnectionFactory connFact = jmsFact.createConnectionFactory();
// Configure the connection factory
connFact.setBusName(busName);
connFact.setProviderEndpoints(providerEndpoints);
connFact.setTargetTransportChain(targetTransportChain);
// Create the connection
Connection conn = connFact.createConnection();
Session session = null;
MessageProducer producer = null;
try {
// Create a session
session = conn.createSession(false, // Not transactional
Session.AUTO_ACKNOWLEDGE);
// Create a message producer
producer = session.createProducer(dest);
// Loop reading lines of text from the console to send
System.out.println("Ready to send to " + dest + " on bus " + busName);
BufferedReader lineInput = new BufferedReader(new InputStreamReader(System.in));
String line = lineInput.readLine();
while (line != null && line.length() > 0) {
// Create a text message containing the line
TextMessage message = session.createTextMessage();
message.setText(line);
// Send the message
producer.send(message,
Message.DEFAULT_DELIVERY_MODE,
Message.DEFAULT_PRIORITY,
Message.DEFAULT_TIME_TO_LIVE);
// Read the next line
line = lineInput.readLine();
}
}
// Finally block to ensure we close our JMS objects
finally {
// Close the message producer
try {
if (producer != null) producer.close();
}
catch (JMSException e) {
System.err.println("Failed to close message producer: " + e);
}
// Close the session
try {
if (session != null) session.close();
}
catch (JMSException e) {
System.err.println("Failed to close session: " + e);
}
// Close the connection
try {
conn.close();
}
catch (JMSException e) {
System.err.println("Failed to close connection: " + e);
}
}
}
}