谁熟悉WEBLOGIC6.0?请看下面的问题

yadang 2001-11-01 01:22:12
请看下面的关于消息驱动的EJB,为什么当客户端发送一条消息后,在服务器的onMessage()方法总是收到2条消息??

EJB2。0的程序:
package examples.ejb20.message;

import weblogic.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

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

public class MessageTraderBean implements MessageDrivenBean, MessageListener {

private static final boolean VERBOSE = true;
private MessageDrivenContext m_context;
private int m_tradeLimit;

private void log(String s) {
if (VERBOSE) System.out.println(s);
}

public void ejbActivate() {
log("ejbActivate called");
}

public void ejbRemove() {
log("ejbRemove called");
}

public void ejbPassivate() {
log("ejbPassivate called");
}

public void setMessageDrivenContext(MessageDrivenContext ctx) {
log("setMessageDrivenContext called");
m_context = ctx;
}

public void ejbCreate () throws CreateException {
log("ejbCreate called");

}

public void onMessage(Message msg) {
TextMessage tm = (TextMessage) msg;
try {
String text = tm.getText();
log("Received new quote : " + text);
}
catch(JMSException ex) {
ex.printStackTrace();
}
}

客户端的程序:
package examples.ejb20.message;

import java.rmi.RemoteException;
import java.util.Properties;

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.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class Client {
static private String TOPIC_NAME = "quotes";

private String m_url;

private Context m_context;
private TopicConnection m_topicConnection;

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

try {
m_context = getInitialContext();

TopicConnectionFactory cf =
(TopicConnectionFactory) m_context.lookup("weblogic.jms.ConnectionFactory");
m_topicConnection = cf.createTopicConnection();
m_topicConnection.start();

}
catch(Exception ex) {
ex.printStackTrace();
}
}

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

log("\nBeginning message.Client...\n");

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

// Parse the argument list
if (args.length != 1) {
System.out.println("Usage: java examples.ejb20.message.Client t3://hostname:port");
return;
} else {
url = args[0];
}

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

try {
client.example();
}
catch (Exception e) {
log("There was an exception while creating and using the Trader.");
log("This indicates that there was a problem communicating with the server: "+e);
//e.printStackTrace();
}

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

public void example()
throws RemoteException, JMSException, NamingException
{
Topic newTopic = null;
TopicSession session = null;
try {
session =
m_topicConnection.createTopicSession(false, // non transacted
Session.AUTO_ACKNOWLEDGE);

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);
TextMessage tm = session.createTextMessage();
String[] quotes = new String[] {
"BEAS 40 1/8", "SUNW 79 1/2", "IBM 82 1/4"
};
for (int i = 0; i < /*quotes.length*/1; i++) {
tm.setText(quotes[i]);
sender.publish(tm);
}
}


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;
}
}

private static void log(String s) {
System.out.println(s);
}

}
...全文
66 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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