1、MQ上新建QM和MQ
QM:WBRK_QM
MQ:Q1(本地队列)
2、在WAS上配置JNDI的JMS队列连接工厂,资源——JMS——队列连接工厂
2.1、选择一个作用域 2.2、点击【新建】 2.3、选择 JMS 资源提供程序,这里选 WebSphere MQ messaging provider
2.4、点击【确定】 2.5、填写【名称】和【JNDI名称】
2.6、点击【下一步】
2.7、继续点击【下一步】,输入队列管理器名称
2.8、点击【下一步】, 【传输】选“客户机” 【主机名】写MQ所在主机地址 【端口】写MQ监听器的端口,默认1414 【服务器连接通道】,写连接通道的名字
2.9、点击【测试连接】,并测试成功
2.10、摘要,并点击【完成】
3、配置队列JNDI 3.1、点击【新建】
3.2、 选择 JMS 资源提供程序
3.3、点击【下一步】、填写各项
3.4、点击【WebSphere MQ 连接属性】,填写各个属性
3.5、点击【确定】。
4、编写发送方程序
package jmsdemo.client; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JMSClientProducer { public static void main(String[] args) { ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination destination = null; // 它有两个子接口 TemporaryQueue 和 TemporaryTopic 见《精通MQ》P249 MessageProducer messageProducer = null; try { Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); p.put(Context.SECURITY_AUTHENTICATION, "simple"); p.put(Context.SECURITY_PRINCIPAL, "LZC"); p.put(Context.SECURITY_CREDENTIALS, "999999999"); p.put(Context.URL_PKG_PREFIXES, "com.ibm.ws.naming"); p.put(Context.PROVIDER_URL, "iiop://127.0.0.1:2813/"); Context context = new InitialContext(p); Object jndi_ConnectionFactory = context.lookup("MQConnectionFactoryDEMO"); Object jndi_Destionation = context.lookup("队列DEMO"); connectionFactory = (ConnectionFactory) jndi_ConnectionFactory; destination = (Destination) jndi_Destionation; connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); messageProducer = session.createProducer(destination); messageProducer.setTimeToLive(100000); BytesMessage message = session.createBytesMessage(); message.setStringProperty("MsgID", "MsgID_001"); message.writeBytes("MESSAGECONTENT".getBytes("GBK")); message.setJMSCorrelationID("CorrelationID_001"); messageProducer.send(message); } catch ( NamingException e ) {} catch ( JMSException e ) {} catch ( UnsupportedEncodingException e ) {} finally { if ( connection != null ) { if ( messageProducer != null ) { try { messageProducer.close(); } catch (JMSException e) { e.printStackTrace(); } } if ( session != null ) { try { session.close(); } catch (JMSException e) { e.printStackTrace(); } } try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } }
5、编写接收方程序
package jmsdemo.client;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JMSClientConsumer
{
public static void main(String[] args)
{
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageConsumer messageConsumer = null;
try
{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
p.put(Context.SECURITY_AUTHENTICATION, "simple");
p.put(Context.SECURITY_PRINCIPAL, "LZC");
p.put(Context.SECURITY_CREDENTIALS, "999999999");
p.put(Context.URL_PKG_PREFIXES, "com.ibm.ws.naming");
p.put(Context.PROVIDER_URL, "iiop://127.0.0.1:2813/");
Context context = new InitialContext(p);
Object jndi_ConnectionFactory = context.lookup("MQConnectionFactoryDEMO");
Object jndi_Destionation = context.lookup("队列DEMO");
connectionFactory = (ConnectionFactory) jndi_ConnectionFactory;
destination = (Destination) jndi_Destionation;
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
messageConsumer = session.createConsumer(destination);
connection.start();
Message m = messageConsumer.receive(1000 * 1);
if ( m instanceof TextMessage )
{
TextMessage textMessage = (TextMessage) m;
System.out.println(textMessage.getText());
}
}
catch ( NamingException e )
{}
catch ( JMSException e )
{}
finally
{
if ( connection != null )
{
if ( messageConsumer != null )
{
try
{
messageConsumer.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
if ( session != null )
{
try
{
session.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
try
{
connection.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
}
}
|