Weblogic JMS 配置及简单实例

1.weblogic (10)配置

(1). 创建domain

(2). 配置 Persistent Stores ,我这里使用filestore

       a. target:AdminServer

       b. Directory: "可访问路径"

(3). 创建JMS Server

       a. Persistent Store:选择(2)配置的

       b. target: AdminServer

(4). 创建JMS Modules

       4.1 进入创建的JMS Modules,切换到subdeployment tab,创建subdeployment, 注意将target配置成(3)的JMS Server

       4.2 切换到 configuration tab,创建 Connection Factory,target:AdminServer

        4.3 同样configuration tab, 创建 queue/topic ,subdeployment配置成(4.1)的subdeployment

 

2. 创建java project编写测试代码

(1) 创建一个java project(不是web项目)

(2) 将weblogic安装目录中的wlclient.jar加入classpath中(注意:weblogic老版本的话使用weblogic.jar), 具体路径:$Weblogic_HOME\wlserver_10.3\server\lib。

(3) 需要将javaee.jar加入到classpath,因为我们建的是java project项目,不会自动添加javaee包。注意:从官网下载javaee sdk,安装后lib中有这个包。

 

Sender:

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class JMSSender {

	/**
	 * @param args
	 * @throws NamingException 
	 * @throws JMSException 
	 */
	public static void main(String[] args) throws NamingException, JMSException {
		//init JNDI context
		String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";//define JNDI context factory
		String providerUrl = "t3://localhost:8001"; //define weblogic JMS url
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory);
		env.put(Context.PROVIDER_URL, providerUrl);
		Context ctx = new InitialContext(env);
		
		//find connection factory
		String connFactoryJNDI = "jms/ConnectionFactoryTest"; //jms connectionFactory JNDI name
		QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(connFactoryJNDI);
		//create queue connection
		QueueConnection qConn = (QueueConnection) connFactory.createConnection();
		//create session
		QueueSession qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		//find queue by JNDI lookup
		Queue queue = (Queue) ctx.lookup("jms/QueueTest");
		//create sender
		QueueSender qSender = qSession.createSender(queue);
		//create message
		Message msg = qSession.createTextMessage("Message is from JMS Sender!");
		qSender.send(msg);
		
		qSender.close();
		
		qSession.close();
		
		qConn.close();
		
	}

}

 

Receiver:

 

import java.util.Hashtable;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class JMSReceiver {
	
	public static void main(String[] args) throws NamingException, JMSException {
		//init JNDI context
		String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";//define JNDI context factory
		String providerUrl = "t3://localhost:8001"; //define weblogic JMS url
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDIFactory);
		env.put(Context.PROVIDER_URL, providerUrl);
		Context ctx = new InitialContext(env);
		
		//find connection factory
		String connFactoryJNDI = "jms/ConnectionFactoryTest"; //jms connectionFactory JNDI name
		QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(connFactoryJNDI);
		//create queue connection
		QueueConnection qConn = (QueueConnection) connFactory.createConnection();
		//create session
		QueueSession qSession = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		//find queue by JNDI lookup
		Queue queue = (Queue) ctx.lookup("jms/QueueTest");
		
		//create receiver
		QueueReceiver qReceiver = qSession.createReceiver(queue);
		//create message listener
		qReceiver.setMessageListener(new MessageListener() {

			public void onMessage(Message msg) {
				String msgText = "";
				double d = 0;
				 try {
					if (msg instanceof TextMessage) {   
					        msgText = ((TextMessage) msg).getText();   
					    } else if (msg instanceof StreamMessage) {   
					        msgText = ((StreamMessage) msg).readString();   
					        d = ((StreamMessage) msg).readDouble();   
					    } else if (msg instanceof BytesMessage) {   
					        byte[] block = new byte[1024];   
					        ((BytesMessage) msg).readBytes(block);   
					        msgText = String.valueOf(block);   
					    } else if (msg instanceof MapMessage) {   
					        msgText = ((MapMessage) msg).getString("name");   
					    } 
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}   
				
				System.out.println(msgText + " " + d);
			}});

		qConn.start();
		
	}
	
}

 

 

 

 

 

 

 

 

你可能感兴趣的:(weblogic,jms)