没有异常,在jb7集成环境里面运行正常(包含用户界面),只是编译成EXE后,消息发程序发不出消息,消息接收程序也收不到消息(Topic里有消息)
贴一个发布程序吧
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;/**
* 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");
m_topicConnection = cf.createTopicConnection();
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://localhost: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); 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", "Hello !"
};
for (int i = 0; i < quotes.length; i++) {
tm.setText(quotes[i]);
sender.publish(tm);
}
}
/**
* Using a Properties object will work on JDK 1.1.x and Java2
* clients
*/
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();
// } private static void log(String s) {
System.out.println(s);
}}
贴一个发布程序吧
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;/**
* 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");
m_topicConnection = cf.createTopicConnection();
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://localhost: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); 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", "Hello !"
};
for (int i = 0; i < quotes.length; i++) {
tm.setText(quotes[i]);
sender.publish(tm);
}
}
/**
* Using a Properties object will work on JDK 1.1.x and Java2
* clients
*/
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();
// } private static void log(String s) {
System.out.println(s);
}}
解决方案 »
- freemarker解析sql或者hql
- 关于BeanUtils的copyProperties()方法
- eclipse 里 web服务问题
- webliic connectionpool 的小问题(在线等)
- hibernate 新手上路,老说没有配方言
- 我对web service的2个小问题,希望大家都进来讨论一下,来者有分
- jsf+tiles框架的配置
- 求一个简单的socket的例子,急!谢!
- 求大神帮我看一下这段代码是什么意思
- springmvc 里包装类pojo绑定数据一直都是null,无法传入值,还请大家帮忙看看
- 建立聊天程序 UDP server 需要用到线程么?如果需要,怎么用?
- 无法使用jbuilder help(jb8)
package jmstest;import javax.jms.*;
import javax.naming.*;
import java.util.*;public class testDurableListener implements MessageListener,Runnable { private String id = ""; private Topic topic = null;
private TopicSession session = null;
private TopicConnectionFactory tcf = null;
private TopicConnection tc = null;
private Context ctx = null;
private TopicSubscriber scriber = null; public String strTopic = "MDBDemoTopic";
public String strFactory = "MDBDemoCF"; public testDurableListener(String id) {
this.id = id;
init();
} private void init(){
try{
ctx = getInitialContext();
tcf = (TopicConnectionFactory)ctx.lookup(this.strFactory);
tc = tcf.createTopicConnection();
tc.setClientID(id);
session = tc.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
topic = (Topic)ctx.lookup(this.strTopic);
scriber = session.createDurableSubscriber(topic,id);
scriber.setMessageListener(this);
tc.start();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
ctx.close();
}catch(Exception exx){}
}
} public void onMessage(Message msg){
try{
TextMessage message = (TextMessage)msg;
System.out.println("##"+id+"##"+message.getText());
}catch(Exception ex){
ex.printStackTrace();
}
} public void run(){
while(true){
synchronized(this){
try{
wait();
}catch(InterruptedException ex){
//ex.printStackTrace();
}
}
}
} public 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, "t3://localhost:7001");
return new InitialContext(h);
}catch (NamingException ex) {
throw ex;
}
}
}//--------------------------------发布消息、main----------------------------------
package jmstest;import javax.jms.*;
import javax.naming.*;
import java.util.*;public class testDurable { private Topic topic = null;
private TopicSession session = null;
private TopicConnectionFactory tcf = null;
private TopicConnection tc = null;
private Context ctx = null;
private TopicPublisher publisher = null; public String strTopic = "MDBDemoTopic";
public String strFactory = "MDBDemoCF"; public testDurable() {
} public static void main(String[] args) {
testDurableListener lis1 = new testDurableListener("1");
Thread t1 = new Thread(lis1);
t1.start(); testDurableListener lis2 = new testDurableListener("2");
Thread t2 = new Thread(lis2);
t2.start(); testDurableListener lis3 = new testDurableListener("3");
Thread t3 = new Thread(lis3);
t3.start(); testDurable durable = new testDurable();
durable.publishMessage();
} public void publishMessage(){
try{
ctx = getInitialContext();
tcf = (TopicConnectionFactory)ctx.lookup(this.strFactory);
tc = tcf.createTopicConnection();
session = tc.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
topic = (Topic)ctx.lookup(this.strTopic);
publisher = session.createPublisher(topic);
Message msg = session.createTextMessage("hhhhhhhhhhhhhhhhhhhhhhhhh");
msg.setStringProperty("name","john");
msg.setStringProperty("age","26");
publisher.publish(msg);
System.out.println("publish complete ");
tc.close();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
ctx.close();
}catch(Exception exx){}
}
} public 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, "t3://localhost:7001");
return new InitialContext(h);
}catch (NamingException ex) {
throw ex;
}
}}
我想肯定是有异常的,只不过你运行该exe文件时,只有那个swing界面而没有控制台窗口,所以本应打印在控制台窗口里的异常你就看不到了。
我的方案
1. 将你的程序打包成XXX.jar文件,用java -jar XXX.jar运行之,看看结果如何。
2. 如果是什么not find之类的异常的话,一般就是classpath问题了。
用你的方法,我的确看到找不到异常:weblogic.jndi.WLInitialContextFactory
但是我在classpath里面已经加上weblogic.jar的路径了,而且在jb7环境下运行正常。如何再编译成EXE过程中引入weblogic.jndi.WLInitialContextFactory?