急:請教在MDB中向另一臺機器發訊息session不能建立

xfzhu2003發表於2003-11-19
我建了一個mdb,名為ContractDraftBean,打算叫它轉發訊息,但是session不能建立:

qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);


異常如下,哪位大蝦幫忙看看,使用平臺為weblogic 7.0 (jdk1.3.1_02_b02):


weblogic.jms.common.JMSException: Connection not found
at weblogic.jms.dispatcher.InvocableManager.invocableFind(InvocableManager.ja
va:134)
at weblogic.jms.dispatcher.Request.wrappedFiniteStateMachine(Request.java:539
)
at weblogic.jms.dispatcher.DispatcherImpl.dispatchSync(DispatcherImpl.java:27
5)
at weblogic.jms.client.JMSConnection.createSession(JMSConnection.java:374)
at weblogic.jms.client.JMSConnection.createQueueSession(JMSConnection.java:33
4)
at untitled1.MessageHelper.<init>(ContractDraftBean.java:69)
at untitled1.ContractDraftBean.onMessage(ContractDraftBean.java:21)
at weblogic.ejb20.internal.MDListener.execute(MDListener.java:348)
at weblogic.ejb20.internal.MDListener.transactionalOnMessage(MDListener.java:
282)
at weblogic.ejb20.internal.MDListener.onMessage(MDListener.java:263)
at weblogic.jms.client.JMSSession.onMessage(JMSSession.java:2309)
at weblogic.jms.client.JMSSession.execute(JMSSession.java:2232)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:152)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:133)
haha


客戶端程式:
package untitled1;

import javax.ejb.*;
import javax.jms.*;
import java.util.*;
import javax.naming.*;

public class MSGTEST{
private InitialContext ic;
private QueueConnectionFactory qConnectionFactory;
private QueueConnection qConnection;
private QueueSession qSession;
private QueueSender qSender;
private Queue queue;
private TextMessage msg;

private final String JNDI_FACTORY =
"weblogic.jndi.WLInitialContextFactory";
private String JMS_FACTORY = null;
private String QUEUE = null;
private String URL = null;

public MSGTEST(String url,
String factoryName, String queueName) {
try {
this.URL = url;
this.JMS_FACTORY = factoryName;
this.QUEUE = queueName;

ic = getInitialContext(URL);
qConnectionFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
qConnection = qConnectionFactory.createQueueConnection();
qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDG
E);
queue = (Queue) ic.lookup(queueName);
qSender = qSession.createSender(queue);
msg = qSession.createTextMessage();
qConnection.start();
}
catch (Exception e) {
e.printStackTrace();
}
}

private InitialContext getInitialContext(String url) throws
NamingException {
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
properties.put(Context.PROVIDER_URL, url);
return new InitialContext(properties);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}

public void sendMessage(String message) throws JMSException {
try {
msg.setText(message);
qSender.send(msg);
}
catch (Exception ex) {
throw new JMSException(ex.getMessage());
}
finally {
close();
}
}

public void close() throws JMSException {
qSender.close();
qSession.close();
qConnection.close();
}

public static void main(String[] args) {
MSGTEST messageHelper = new MSGTEST("t3://localhost:7001",
"ContractDraftCF", "ContractDraftQueue");
try {
messageHelper.sendMessage("haha");
}
catch (Exception e) {
e.printStackTrace();
}
}
}


訊息驅動bean程式如下:
package untitled1;

import javax.ejb.*;
import javax.jms.*;
import java.util.*;
import javax.naming.*;

public class ContractDraftBean implements MessageDrivenBean, MessageListener {

MessageDrivenContext messageDrivenContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void onMessage(Message msg) {
try {
TextMessage tm=(TextMessage) msg;
String str = tm.getText();
System.out.println(str);
MessageHelper messageHelper = new MessageHelper("t3://pl:7001",
"ContractDraftCF", "ContractDraftQueue");
messageHelper.sendMessage(str);
}
catch (JMSException e) {
e.printStackTrace();
}
}
public void setMessageDrivenContext(MessageDrivenContext messageDrivenContex
t) {
this.messageDrivenContext = messageDrivenContext;
}
}

class MessageHelper {
private InitialContext ic;
private QueueConnectionFactory qConnectionFactory;
private QueueConnection qConnection;
private QueueSession qSession;
private QueueSender qSender;
private Queue queue;
private TextMessage msg;

private final String JNDI_FACTORY =
"weblogic.jndi.WLInitialContextFactory";
private String JMS_FACTORY = null;
private String QUEUE = null;
private String URL = null;

public MessageHelper(String url,
String factoryName, String queueName) {
try {
this.URL = url;
this.JMS_FACTORY = factoryName;
this.QUEUE = queueName;

ic = getInitialContext(URL);
qConnectionFactory = (QueueConnectionFactory) ic.lookup(JMS_FACTORY);
qConnection = qConnectionFactory.createQueueConnection();
qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDG
E);
queue = (Queue) ic.lookup(queueName);
qSender = qSession.createSender(queue);
msg = qSession.createTextMessage();
qConnection.start();
}
catch (Exception e) {
e.printStackTrace();
}
}

private InitialContext getInitialContext(String url) throws
NamingException {
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
properties.put(Context.PROVIDER_URL, url);
return new InitialContext(properties);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}

public void sendMessage(String message) throws JMSException {
try {
msg.setText(message);
qSender.send(msg);
}
catch (Exception ex) {
throw new JMSException(ex.getMessage());
}
finally {
close();
}
}

public void close() throws JMSException {
qSender.close();
qSession.close();
qConnection.close();
}

public static void main(String[] args) {
MessageHelper messageHelper = new MessageHelper("t3://pl:7001",
"ContractDraftCF", "ContractDraftQueue");
try {
messageHelper.sendMessage("haha");
}
catch (Exception e) {
e.printStackTrace();
}
}
}

相關文章