WEBLOGIC中JMS收发消息

一个 JMS 应用程序由下列元素组成:
· JMS 客户机。 用 JMS API 发送和接收消息的 Java 程序。
· 非 JMS(Non-JMS)客户机。 认识到这一点很重要 - 旧的程序经常成为整个 JMS 应用程序的一部分,而且它们的包含应该在设计时预先考虑。
· 消息。 在 JMS 和非 JMS 客户机之间交换的消息的格式和内容是 JMS 应用程序设计所必须考虑的部分。
· JMS 供应商。供应商必须提供特定于其 MOM 产品的具体的实现。
· 受管对象。 消息传递系统供应商的管理员创建了一个对象,它独立于供应商专有的技术。包括连接工厂ConnectionFactory和目的Destination。
一种典型的 JMS 程序需要经过下列步骤才能开始消息产生和使用:
· 通过 JNDI 查找 ConnectionFactory。
· 通过 JNDI 查找一个或多个 Destination。
· 用 ConnectionFactory 创建一个 Connection。
· 用 Connection 创建一个或多个 Session。
· 用 Session 和 Destination 创建所需的 MessageProducer 和 MessageConsumer。
· 启动 Connection。
下面利用上面配置的JMS资源演示点对点消息发送和接收的过程。
五. 设计消息发送端
1. 使用的JMS资源
服务器URL: t3://localhost:80
连接工厂: SendJMSFactory
队列: SendJMSQueue
2. 设计步骤
· 初始化JNDI Tree
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
· lookup ConnectionFactory
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
· lookup Destination
queue = (Queue) ctx.lookup(queueName);
· 用 ConnectionFactory 创建Connection
qcon = qconFactory.createQueueConnection();
· 用 Connection 创建一个Session
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
· 用 Session 和 Destination 创建MessageProducer
qsender = qsession.createSender(queue);
· 启动 Connection。
qcon.start();
· 发送消息
msg = qsession.createTextMessage();
msg.setText(message);
qsender.send(msg);
3. 源代码

package jmssample; 
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/** This example shows how to establish a connection
* and send messages to the JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages. The class is used to send messages to the queue.
*
* @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
*/
public class QueueSend
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

// Defines the JNDI provider url.
public final static String PROVIDER_URL=" t3://localhost:80";

// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY="SendJMSFactory";

// Defines the queue.
public final static String QUEUE="SendJMSQueue";


private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;

/**
* Creates all the necessary objects for sending
* messages to a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}

/**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}

/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
/** main() method.
*
* @param args WebLogic Server URL
* @exception Exception if operation fails
*/
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}

private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);

}

private static InitialContext getInitialContext()
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}

}

 

 

六. 设计消息接收端
1. 使用的JMS资源
服务器URL: t3://localhost:80
连接工厂: SendJMSFactory
队列: SendJMSQueue
2. 设计步骤
· 初始化JNDI Tree
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
· lookup ConnectionFactory
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
· lookup Destination
queue = (Queue) ctx.lookup(queueName);
· 用 ConnectionFactory 创建Connection
qcon = qconFactory.createQueueConnection();
· 用 Connection 创建一个Session
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
· 用 Session 和 Destination 创建MessageConsumer
qreceiver = qsession.createReceiver(queue);
· 设置监听
qreceiver.setMessageListener(this);
· 启动 Connection
qcon.start();
3. 源代码

package jmssample;

import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
* This example shows how to establish a connection to
* and receive messages from a JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages. This class is used to receive and remove messages
* from the queue.
*
* @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
*/
public class QueueReceive implements MessageListener
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

// Defines the JNDI provider url.
public final static String PROVIDER_URL=" t3://localhost:80";

// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY="SendJMSFactory";

// Defines the queue.
public final static String QUEUE="SendJMSQueue";

private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;

/**
* Message listener interface.
* @param msg message
*/
public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}

System.out.println("Message Received: "+ msgText );

if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

/**
* Creates all the necessary objects for receiving
* messages from a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
}

/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close()throws JMSException
{
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args WebLogic Server URL
* @exception Exception if execution fails
*/

public static void main(String[] args) throws Exception {

InitialContext ic = getInitialContext();
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE);

System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");

// Wait until a "quit" message has been received.
synchronized(qr) {
while (! qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {}
}
}
qr.close();
}

private static InitialContext getInitialContext()
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}


}

 

 

七. 测试消息发送和接收
1)启动cmd 进入weblogic81\server\bin中输入setWSLenv.cmd

设置好环境变量后,进入jmssmaple中javac编译代码,再执行java 运行接收端。

2)再次启动一个cmd 进入weblogic81\server\bin中输入setWSLenv.cmd

设置好环境变量后,进入jmssmaple中执行java 运行发送端。

这样就可以完成收发消息了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值