异步消息的接收问题:)谢谢!!

dl1125 2003-06-03 02:21:52
客户端发送基于主题(TOPIC)信息,请问在public void onMessage(Message msg) 中可以接收吗?谢谢!!我写了一下:收不到!
public void onMessage(Message msg) {
try {
TextMessage tm = (TextMessage) msg;
String s = tm.getText();
System.out.print("okokok");
System.out.print(s);
}
catch (Exception ex) {
ex.printStackTrace();
}
/**@todo Complete this method*/
}
...全文
38 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
phantomhu 2003-06-14
  • 打赏
  • 举报
回复
//本类需要导入的其他类或包
//Java 基础类库中的类
import java.io.*;
import java.util.*;
//Java扩展类库中的包,依次是名称服务器类和Java消息包
import javax.naming.*;
import javax.jms.*;

/**
* 这个实例描述怎样和消息服务器上的一个消息主题建立连接,以及怎样接收这个
* 主题的消息。创建一个消息主题接收,可以分为三个步骤,在类TopicReceive中,
* 这三个步骤封装成了三个方法,在main方法中依次调用。
*/

//消息接收程序必须实现MessageListener接口,才能接收到发送的消息
public class TopicReceive
implements MessageListener
{
/**
* 定义JNDI构造器
*/

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
/**
* 定义JMS连接器
*/
public final static String JMS_FACTORY="EIS.MeetingFactory";
/**
* 定义主题
*/
public final static String TOPIC="EIS.MeetingTopic";
//声明主题消息连接器变量
private TopicConnectionFactory tconFactory;
//声明主题消息连接变量
private TopicConnection tcon;
//声明主题消息会话器变量
private TopicSession tsession;
//声明主题消息接收器变量
private TopicSubscriber tsubscriber;
//声明主题消息变量
private Topic topic;
private boolean quit = false;

/**
* 必须实现的接口MessageListener中的方法,这个方法被消息服务器自动激发。
* @参数 msg 消息
*
*/
public void onMessage(Message msg)
{
try {
//声明文本消息变量
String msgText;
//判断消息是否为文本消息实例
if (msg instanceof TextMessage) {
//是,则取出消息文本赋值到变量
msgText = ((TextMessage)msg).getText();
} else {
//不是,则用toString()方法取出这个消息的信息
msgText = msg.toString();
}
//在控制台显示
System.out.println("JMS 消息接收: "+ msgText );

if (msgText.equalsIgnoreCase("quit")) {
//如果发出退出消息,则置退出标志为真,使本程序终止运行
synchronized(this) {
quit = true;
this.notifyAll(); // 通知主现成退出
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}

/**
* 创建发送主体消息相关的对象
*
* @参数 ctx JNDI 上下文
* @参数 topicName 主题名
* @异常 NamingExcpetion JNDI异常
* @异常 JMSException if JMS异常
*
*/
public void init(Context ctx, String topicName)
throws NamingException, JMSException
{
//消息上下文从JNDI服务中寻找到主题连接构造器
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
System.out.println("TopicConnectionFactory OK");
//建立主题连接
tcon = tconFactory.createTopicConnection();
System.out.println("TopicConnection ok");
//创建主题会话
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("TopicSession ok");
//消息上下文从JNDI服务中寻找到主题
topic = (Topic) ctx.lookup(topicName);
System.out.println("Topic ok");
//在主体会话中创建主题接收器
tsubscriber = tsession.createDurableSubscriber(topic,"phantom");
System.out.println("DurableSubscriber ok");
//设置主题接收的消息监听器
tsubscriber.setMessageListener(this);
//启动连接
tcon.start();
}

/**
* 关闭 JMS 对象.
*
* @异常 JMSException JMS 异常
*/
public void close()
throws JMSException
{
tsubscriber.close();
tsession.close();
tcon.close();
}
/**
* 主方法
*
* @参数 args WebLogic服务器URL
* @异常 Exception 操作失误
*/

public static void main(String[] args)
throws Exception
{
//创建初始化上下文实例
InitialContext ic = getInitialContext("t3://127.0.0.1:7001/");
//声明本类的一个实例
TopicReceive tr = new TopicReceive();
//建立给定主题的消息服务器连接
tr.init(ic, TOPIC);

System.out.println("JMS 准备好了接收消息(要退出, 发送一个\"quit\"消息).");

// 等待,发出直到"quit"消息
synchronized(tr) {
while (! tr.quit) {
try {
tr.wait();
} catch (InterruptedException ie) {}
}
}
tr.close();
}
/**
* 得到初始化的JNDI上下文
*
* @参数 url 消息服务器
* @异常 NamingException JNDI错误抛出的异常
*/
private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
env.put("weblogic.jndi.createIntermediateContexts", "true");
return new InitialContext(env);
}

}



jeston 2003-06-11
  • 打赏
  • 举报
回复
newTopic.setMessageListerner(this)
dl1125 2003-06-03
  • 打赏
  • 举报
回复
控制台显示的:
*** <MessageTraderBean> Received new quote : dd
但是public void onMessage(Message msg)方法好像没有调用到!
dl1125 2003-06-03
  • 打赏
  • 举报
回复
客户端的:
package jmstest;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/


import java.rmi.RemoteException;

import java.util.Properties;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;
import java.io.*;


/**

* This class illustrates calling a Message-Driven bean and publishing

* quotes on a topic.

*

* @author Copyright (c) 1998-2002 by BEA Systems, Inc. All Rights Reserved.

*/
public class TestClient {
static private String TOPIC_NAME = "MDBDemo Topic";
private String m_url;
private Context m_context;
private TopicConnection m_topicConnection;

public TestClient(String url) throws NamingException {
m_url = url;

try {
//
// Create a context
//
m_context = getInitialContext();

//
// Create the connection and start it
//
TopicConnectionFactory cf = (TopicConnectionFactory) m_context.lookup(
"MDBDemoCF");
//查找JMS工厂
m_topicConnection = cf.createTopicConnection();
//创建一个topicConnection实例
m_topicConnection.start();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

/**

* Runs this example from the command line. Example:

* <p>

* <tt>java examples.ejb20.message.Client "t3://localhost:7001"</tt>

* <p>

* The parameters are optional, but if any are supplied,

* they are interpreted in this order:

* <p>

* @param url URL such as "t3://localhost:7001" of Server

*/
public static void main(String[] args) throws Exception {
log("\nBeginning message.Client...\n");

String url = "t3://dl:7001";

TestClient client = null;

try {
client = new TestClient(url);
} catch (NamingException ne) {
System.exit(1);
}

try {
client.example();
}
catch (Exception e) {
log("There was an exception while creating and using the MDB.");

log(
"This indicates that there was a problem communicating with the server: " +
e);

//e.printStackTrace();
}

log("\nEnd message.Client...\n");
}

/**

* Runs this example.

*/
public void example() throws RemoteException, JMSException, NamingException {
//真正的方法的实现
Topic newTopic = null;

TopicSession session = null;

try {
session = m_topicConnection.createTopicSession(false, // non transacted
Session.AUTO_ACKNOWLEDGE);
//创建一个Session 的实例
newTopic = (Topic) m_context.lookup(TOPIC_NAME);
//查找主题引用
}
catch (NamingException ex) {
newTopic = session.createTopic(TOPIC_NAME);

m_context.bind(TOPIC_NAME, newTopic);
}

TopicPublisher sender = session.createPublisher(newTopic);
//为主题创建一个TopicPublisher实例
TextMessage tm = session.createTextMessage();
// 创建一个消息类型
/**
String[] quotes = new String[] {
"<name>段炼</name>"
};

for (int i = 0; i < quotes.length; i++) {
tm.setText(quotes[i]);
sender.publish(tm);
//发送消息
}
*/
StringBuffer btemp=readxml();
//tm.setText(btemp.toString());
tm.setText("dd");
sender.publish(tm);

}


/**

* Using a Properties object will work on JDK 1.1.x and Java2

* clients

*/

public StringBuffer readxml(){
StringBuffer sb = new StringBuffer();
try
{
FileInputStream in = new FileInputStream("G:/Coad/JMSTest/classes/jmstest/registry.xml");
DataInputStream datain = new DataInputStream(in);
String s1= new String();

while((s1=datain.readLine())!=null)
{
sb.append(s1);
}
return sb;
}
catch(FileNotFoundException e)
{
System.err.println("FileNotFoundExceprion");
}
catch(IOException e)
{
System.err.println("Exception");
}
return sb;
}

private static void log(String s) {
System.out.println(s);
}
private Context getInitialContext() throws NamingException {
try {
// Get an InitialContext
Properties h = new Properties();

h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");

h.put(Context.PROVIDER_URL, m_url);

return new InitialContext(h);
}
catch (NamingException ex) {
log("We were unable to get a connection to the WebLogic server at " +
m_url);

log("Please make sure that the server is running.");

throw ex;
}
}


/**

* This is the Java2 version to get an InitialContext.

* This version relies on the existence of a jndi.properties file in

* the application's classpath.

*

*/

// private static Context getInitialContext()
// throws NamingException
// {
// return new InitialContext();
// }
}
dl1125 2003-06-03
  • 打赏
  • 举报
回复
谢谢!!

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧