weblogic jms

WebLogic11gR1下载地址 http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main-097127.html

 

下载完成安装后,新建一个weblogic server,启动server,在http://localhost:7001/console页面上配置jms模块和jms服务。

 

下面是代码:

 

1.P2P方式发送接收消息:

    sender:

 

package com.jms.producer;

import java.util.Hashtable;

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

public class JMSQueueSender {

	private static InitialContext initialContext = null;
	private static QueueConnectionFactory qcf = null;
	private static QueueConnection qc = null;
	private static QueueSession session = null;
	private static Queue queue = null;
	private static QueueSender sender = null;
	private static TextMessage textMessage = null;
	
	private static final String QCF_NAME = "jms/QueueConnectionFactory";
	private static final String QUEUE_NAME = "jms/MyQueue";
	
	/**
	 * Init JMS
	 */
	private void initJMS() {
		Hashtable<String, String> properties = new Hashtable<String, String>();
		
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
		properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
		properties.put(Context.SECURITY_CREDENTIALS, "12345678");
		
		try {
			initialContext = new InitialContext(properties);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}

		// Create QueueConnectionFactory
		try {
			qcf = (QueueConnectionFactory) initialContext.lookup(QCF_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create QueueConnection
		try {
			qc = qcf.createQueueConnection();
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create QueueSession
		try {
			session = qc.createQueueSession(false, 0);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Queue
		try {
			queue = (Queue) initialContext.lookup(QUEUE_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create sender
		try {
			sender = session.createSender(queue);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create TextMessage
		try {
			textMessage = session.createTextMessage();
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
	}
	
	/**
	 * Clean up JMS
	 */
	private void clean() {
		try {
			textMessage = null;
			sender.close();
			sender = null;
			queue = null;
			session.close();
			session = null;
			qc.close();
			qc = null;
			qcf = null;
			initialContext.close();
			initialContext = null;
		} catch (JMSException e) {
			e.printStackTrace(System.err);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
		}
		
	}
	
	/**
	 * send message method
	 */
	private void sendMessage(String message) {
		initJMS();
		
		// Set TextMessage
		try {
			textMessage.setText(message);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Send message
		try {
			sender.send(textMessage);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		clean();
	}
	
	public static void main(String[] args) {
		JMSQueueSender producer = new JMSQueueSender();
		
		producer.sendMessage("This is a queue JMS test!");
	}
}

 

   receiver:

package com.jms.receive;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class JMSQueueReceiver {

	private static InitialContext initialContext = null;
	private static QueueConnectionFactory qcf = null;
	private static QueueConnection qc = null;
	private static QueueSession session = null;
	private static Queue queue = null;
	private static QueueReceiver receiver = null;
	private static TextMessage textMessage = null;
	
	private static final String QCF_NAME = "jms/QueueConnectionFactory";
	private static final String QUEUE_NAME = "jms/MyQueue";
	
	/**
	 * Init JMS
	 */
	private void initJMS() {
		Hashtable<String, String> properties = new Hashtable<String, String>();
		
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
		properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
		properties.put(Context.SECURITY_CREDENTIALS, "12345678");
		
		try {
			initialContext = new InitialContext(properties);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}

		// Create QueueConnectionFactory
		try {
			qcf = (QueueConnectionFactory) initialContext.lookup(QCF_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create QueueConnection
		try {
			qc = qcf.createQueueConnection();
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create QueueSession
		try {
			session = qc.createQueueSession(false, 0);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Queue
		try {
			queue = (Queue) initialContext.lookup(QUEUE_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Reveiver
		try {
			receiver = session.createReceiver(queue);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
	}
	
	/**
	 * Clean up JMS
	 */
	private void clean() {
		try {
			textMessage = null;
			receiver.close();
			receiver = null;
			queue = null;
			session.close();
			session = null;
			qc.close();
			qc = null;
			qcf = null;
			initialContext.close();
			initialContext = null;
		} catch (JMSException e) {
			e.printStackTrace(System.err);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
		}
		
	}
	
	/**
	 * send message method
	 */
	private void receiveMessage() {
		initJMS();
		
		// Start QueueConnection
		try {
			qc.start();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		// Receive Message
		try {
			textMessage = (TextMessage) receiver.receive();
			System.out.println(textMessage.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		clean();
	}
	
	public static void main(String[] args) {
		JMSQueueReceiver myJMSReceiver = new JMSQueueReceiver();
		
		myJMSReceiver.receiveMessage();
	}
}

 

 

 

 

2.pub/sub方式发送、接收消息

   publisher:

package com.jms.producer;

import java.util.Hashtable;

import javax.jms.JMSException;
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.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSTopicPublisher {

	private static InitialContext initialContext = null;
	private static TopicConnectionFactory tcf = null;
	private static TopicConnection tc = null;
	private static TopicSession session = null;
	private static Topic topic = null;
	private static TopicPublisher publiser = null;
	private static TextMessage textMessage = null;
	
	private static final String TCF_NAME = "jms/QueueConnectionFactory";
	private static final String TOPIC_NAME = "jms/MyTopic";
	
	/**
	 * Init JMS
	 */
	private void initJMS() {
		Hashtable<String, String> properties = new Hashtable<String, String>();
		
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
		properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
		properties.put(Context.SECURITY_CREDENTIALS, "12345678");
		
		try {
			initialContext = new InitialContext(properties);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}

		// Create TopicConnectionFactory
		try {
			tcf = (TopicConnectionFactory) initialContext.lookup(TCF_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create TopicConnection
		try {
			tc = tcf.createTopicConnection();
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create TopicSession
		try {
			session = tc.createTopicSession(false, 0);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Topic
		try {
			topic = (Topic) initialContext.lookup(TOPIC_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Publisher
		try {
			publiser = session.createPublisher(topic);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create TextMessage
		try {
			textMessage = session.createTextMessage();
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
	}
	
	/**
	 * Clean up JMS
	 */
	private void clean() {
		try {
			textMessage = null;
			publiser.close();
			publiser = null;
			topic = null;
			session.close();
			session = null;
			tc.close();
			tc = null;
			tcf = null;
			initialContext.close();
			initialContext = null;
		} catch (JMSException e) {
			e.printStackTrace(System.err);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
		}
		
	}
	
	/**
	 * Publisher message
	 */
	private void publishMessage(String message) {
		initJMS();
		
		// Set TextMessage
		try {
			textMessage.setText(message);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Publisher message
		try {
			publiser.publish(textMessage);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		clean();
	}
	
	public static void main(String[] args) {
		JMSTopicPublisher producer = new JMSTopicPublisher();
		
		producer.publishMessage("This is a topic JMS test!");
	}
}

 

  subscriber:

package com.jms.receive;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class JMSTopicSubscriber {

	private static InitialContext initialContext = null;
	private static TopicConnectionFactory tcf = null;
	private static TopicConnection tc = null;
	private static TopicSession session = null;
	private static Topic topic = null;
	private static TopicSubscriber subscriber = null;
	private static TextMessage textMessage = null;
	
	private static final String TCF_NAME = "jms/QueueConnectionFactory";
	private static final String TOPIC_NAME = "jms/MyTopic";
	
	/**
	 * Init JMS
	 */
	private void initJMS() {
		Hashtable<String, String> properties = new Hashtable<String, String>();
		
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
		properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
		properties.put(Context.SECURITY_CREDENTIALS, "12345678");
		
		try {
			initialContext = new InitialContext(properties);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}

		// Create TopicConnectionFactory
		try {
			tcf = (TopicConnectionFactory) initialContext.lookup(TCF_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create TopicConnection
		try {
			tc = tcf.createTopicConnection();
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create TopicSession
		try {
			session = tc.createTopicSession(false, 0);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Topic
		try {
			topic = (Topic) initialContext.lookup(TOPIC_NAME);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
		// Create Subscriber
		try {
			subscriber = session.createSubscriber(topic);
		} catch (JMSException e) {
			e.printStackTrace(System.err);
			System.exit(0);
		}
		
	}
	
	/**
	 * Clean up JMS
	 */
	private void clean() {
		try {
			textMessage = null;
			subscriber.close();
			subscriber = null;
			topic = null;
			session.close();
			session = null;
			tc.close();
			tc = null;
			tcf = null;
			initialContext.close();
			initialContext = null;
		} catch (JMSException e) {
			e.printStackTrace(System.err);
		} catch (NamingException e) {
			e.printStackTrace(System.err);
		}
		
	}
	
	/**
	 * send message method
	 */
	private void receiveMessage() {
		initJMS();
		
		// Start QueueConnection
		try {
			tc.start();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		// Receive Message
		try {
			textMessage = (TextMessage) subscriber.receive();
			System.out.println(textMessage.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		clean();
	}
	
	public static void main(String[] args) {
		JMSTopicSubscriber myJMSReceiver = new JMSTopicSubscriber();
		
		myJMSReceiver.receiveMessage();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值