关闭

学习笔记——JMS示例

1068人阅读 评论(0) 收藏 举报
 

发送消息示例:

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

public class MesssageSender {
	public void sendMessage() throws NamingException, JMSException {
		//定义WebLogic默认连接工厂的JNDI
		final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
		//获取JNDI服务所需的Context
		Context context = getInitialContext();
		//通过JNDI查找获取连接工厂
		ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(CONNECTION_FACTORY_JNDI); 
		//通过JNDI查找获取消息目的
		Destination destination = (Destination)context.lookup("MessageQueue");
		//连接工厂创建连接
		Connection connection = connectionFactory.createConnection();
		//JMS连接创建JMS会话
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//JMS会话创建消息生产者
		MessageProducer sender = session.createProducer(destination);
		//设置消息生产者生产出来的消息的传递模式、有效时间。
		sender.setDeliveryMode(DeliveryMode.PERSISTENT);
		sender.setTimeToLive(20000);
		//通过JMS会话创建一个文本消息
		TextMessage message = session.createTextMessage();
		//message.setStringProperty("ConType","txt");
		message.setText("Hello");
		sender.send(message);
		session.close();
		connection.close();
	}
	
	private Context getInitialContext() {
		final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
		final String SERVER_URL = "t3://localhost:7001";
		Context context = null;
		try {
			Properties properties = new Properties();
			properties.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY);
			properties.put(Context.PROVIDER_URL , SERVER_URL);
			context = new InitialContext(properties);
		}
		catch (NamingException exception) {
			System.err.println("不能连接WebLogic Server在:" + SERVER_URL);
			ne.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		MessageSender messageSender = new MessageSender();
		messageSender.sendMessage();
	}
}


消息同步接收示例:

 

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

public class SyncConsumer {	 
	public void receiveMessage()
		throws JMSException , NamingException {
		//定义WebLogic默认连接工厂的JNDI
		final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
		//获取JNDI服务所需的Context
		Context context = getInitialContext();
		//通过JNDI查找获取连接工厂
		ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(CONNECTION_FACTORY_JNDI);
		//通过JNDI查找获取消息目的	
		Destination destination = (Destination)context.lookup("MessageQueue");
		//连接工厂创建连接
		Connection connection = connectionFactory.createConnection();
		//启动JMS连接,让它开始传输JMS消息
		connection.start();
		//JMS连接创建JMS会话
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//JMS会话创建消息消费者
		MessageConsumer receiver = session.createConsumer(destination);
		//同步接收消息,如果没有接收到消息,该方法会阻塞线程
		TextMessage message = (TextMessage)receiver.receive();
		System.out.println(message);
		System.out.println("同步接收到的消息:" + message.getText());
		//关闭资源
		session.close();
		connection.close();
	}
	//工具方法,用来获取命名服务的Context对象
	private Context getInitialContext() {
		final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
		final String SERVER_URL = "t3://localhost:7001";
		Context context = null;
		try {
			Properties properties = new Properties();
			properties.put(Context.INITIAL_CONTEXT_FACTORY , INIT_FACTORY);
			properties.put(Context.PROVIDER_URL , SERVER_URL);
			context = new InitialContext(properties);
		}
		catch(NamingException ne) {
			System.err.println("不能连接WebLogic Server在:"
				+ SERVER_URL);
			ne.printStackTrace();
		}
		return context;
	}
	public static void main(String[] args)
		throws Exception {
		SyncConsumer mp = new SyncConsumer();
		mp.receiveMessage();
	}
}

 

消息异步接受示例:

 

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

public class AsyncConsumer
	implements MessageListener {
	public AsyncConsumer()throws NamingException
		,JMSException , InterruptedException {
		//定义WebLogic默认连接工厂的JNDI
		final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";
		//获取JNDI服务所需的Context
		Context context = getInitialContext();
		//通过JNDI查找获取连接工厂
		ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(CONNECTION_FACTORY_JNDI); 
		//通过JNDI查找获取消息目的
		Destination destination = (Destination)context.lookup("MessageQueue");
		//连接工厂创建连接
		Connection connection = connectionFactory.createConnection();
		//启动JMS连接,让它开始传输JMS消息
		connection.start();
		//JMS连接创建JMS会话	
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//JMS会话创建消息消费者
		MessageConsumer receiver = session.createConsumer(destination);
		//为JMS消息消费者绑定消息监听器
		receiver.setMessageListener(this);
		//程序暂停20s,在此期间内以异步方式接收消息
		Thread.sleep(20000);
		//关闭资源
		session.close();
		connection.close();
	}
	//实现消息监听器必须实现的方法。
	public void onMessage(Message m) {
		TextMessage message = (TextMessage)m;
		System.out.println(message);
		try {
			System.out.println("异步接收的消息:" + message.getText());
		}
		catch (JMSException ex) {
			ex.printStackTrace();
		}
	}
	//工具方法,用来获取命名服务的Context对象
	private Context getInitialContext() {
		final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
		final String SERVER_URL = "t3://localhost:7001";
		Context context = null;
		try {
			Properties properties = new Properties();
			properties.put(Context.INITIAL_CONTEXT_FACTORY , INIT_FACTORY);
			properties.put(Context.PROVIDER_URL , SERVER_URL);
			context = new InitialContext(properties);
		}
		catch(NamingException ne) {
			System.err.println("不能连接WebLogic Server在:" + SERVER_URL);
			ne.printStackTrace();
		}
		return context;
	}
	public static void main(String[] args)
		throws Exception {
		AsyncConsumer consumer = new AsyncConsumer();
	}
}





   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:75507次
    • 积分:1262
    • 等级:
    • 排名:千里之外
    • 原创:54篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档