wxsheng 发表于 2017-2-14 07:28:42

jms for weblogic

示例代码:

import java.util.Hashtable;
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
public class QueneSend{
/**
* @param args
* @throws NamingException
* @throws JMSException
*/
public static void main(String[] args) throws NamingException, JMSException {
// TODO Auto-generated method stub
String queueName="TestQueue";
System.out.println("QUEUE NAME IS:" +queueName);
Properties properties=new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.setProperty(Context.PROVIDER_URL,"t3://localhost:7001");
Context jndiContext=new InitialContext(properties);
QueueConnectionFactory qFactory=(QueueConnectionFactory)jndiContext.lookup("TestQueueFactory");
QueueConnection qConnection=qFactory.createQueueConnection();
QueueSession qSession=qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue=(Queue)jndiContext.lookup(queueName);
QueueSender qSender=qSession.createSender(queue);
TextMessage message=qSession.createTextMessage();
for (int i = 1; i < 4; i++) {
message.setText("队列"+i+"测试    ");
System.out.println(message.getText());
qSender.send(message);
}
Message message2=qSession.createMessage();
if(qSession!=null)
qSession.close();
}
}


运行条件:
导入:wlclient.jar、wljmsclient.jar两个jar包(在WEBLOGIC安装目录的lib地下,如:bea\weblogic81\server\lib),WEBLOGIC用8.0版本,jdk1.4版本。
配置weblogic工厂步骤:
1、新建jms连接工厂,工厂名称为“TestQueueFactory”:
2、新建jms服务器,服务器名称为:“TestQueueJmsServer”:
3、在“TestQueueJmsServer”服务下新建目标为“TestQueue”队列:

错误解决:
错误1:
显示“javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory ” 为什么编译没问题了,却还是出错呢,这是网络上问得最多的问题。这是缺少"\bea\weblogic81\server\lib\wlclient.jar"文件所致。 在buildpath里加入这个包后再运行。
如果还是出错,显示“Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/corba/se/connection/ORBSocketFactory” 还是缺少包,这时如果你只安装有jre1.5.*,那是没有ORBSocketFactory这个类文件的。还是找个jre1.4.*吧,这个类在“jre/lib/rt.jar”包中。据说这个问题在weblogic9中已解决。
错误2:
java.lang.IllegalAccessError: tried to access method weblogic.kernel.KernelStatus.initialized()V from class weblogic.kernel.Kernel
使用wljmsclient.jar和wlclient.jar替换jms.jar和weblogic.jar
即可
页: [1]
查看完整版本: jms for weblogic