---
title: "EJB：事件驱动 bean ---Topics"
author: "youngmaster"
date: "2009-09-26T18:35:54Z"
tags: []
slug: "ejb-bean-topics"
draft: false
lastmod: "2009-09-26T18:35:54Z"
id: 477996
url: "https://youngmaster.iteye.com/blog/477996"
---

创建一个工具类:

//ContextFactory.java

```
package com.youngmaster.jms.utils;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * @author youngmaster
 * @E-mail:youngmaster.fly@gmail.com
 * @version myEclipse 6.6
 * @create time：Sep 26, 2009 1:55:42 AM
 */
public class ContextFactory {

	@SuppressWarnings("unchecked")
	public static Context getContext() throws Exception {
		Hashtable props = new Hashtable();

		props.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");

		props.put(Context.PROVIDER_URL, "localhost:1099");

		props.put("java.naming.rmi.security.manager", "yes");

		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");

		@SuppressWarnings("unused")
		Context context = new InitialContext(props);
		return context;
	}

}
```

 

发送端：

//Publisher.java

```
package com.youngmaster.jms.publisher;

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.naming.Context;

import com.youngmaster.jms.utils.ContextFactory;

/**
 * @author youngmaster
 * @E-mail:youngmaster.fly@gmail.com
 * @version myEclipse 6.6
 * @create time：Sep 26, 2009 1:21:44 AM
 */
public class Publisher {

	public static void main(String[] args) throws Exception {

		Context context=ContextFactory.getContext();
		TopicConnectionFactory factory = (TopicConnectionFactory) context
		.lookup("ConnectionFactory");
		TopicConnection connection=factory.createTopicConnection();
		TopicSession session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);


		/**
		 * create topic
		 */
		Topic topic = (Topic) context.lookup("topic/testTopic");

		/**
		 * create topicPublisher
		 */
		@SuppressWarnings("unused")
		TopicPublisher topicPublisher = session.createPublisher(topic);

		/**
		 * create TextMessage
		 */
		TextMessage textmessage = session.createTextMessage();
		textmessage.setText("Hello,jms world");

		topicPublisher.send(topic, textmessage);

		session.close();
		connection.close();
	}

}
```

 

接收端：

//Subscriber.jva

```
package com.youngmaster.jms.scriber;

import javax.jms.Session;
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 com.youngmaster.jms.utils.ContextFactory;

/**
 * @author youngmaster
 * @E-mail:youngmaster.fly@gmail.com
 * @version myEclipse 6.6
 * @create time：Sep 26, 2009 1:39:18 AM
 */
public class Subscriber {

	/**
	 * @param args
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		
		Context context=ContextFactory.getContext();
		TopicConnectionFactory factory = (TopicConnectionFactory) context
		.lookup("ConnectionFactory");
		TopicConnection connection=factory.createTopicConnection();
		TopicSession session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

		/**
		 * create topic
		 */
		Topic topic = (Topic) context.lookup("topic/testTopic");

		/**
		 * create topicSubscriber
		 */
		TopicSubscriber topicSubscriber = session.createSubscriber(topic);

		topicSubscriber.setMessageListener(new MySubscriber());
		connection.start();
	}

}
```

 

创建一个监听器：

//MySubscriber.java

```
package com.youngmaster.jms.scriber;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author youngmaster
 * @E-mail:youngmaster.fly@gmail.com
 * @version myEclipse 6.6
 * @create time：Sep 26, 2009 1:40:40 AM
 */
public class MySubscriber implements MessageListener {

	public void onMessage(Message arg0) {

		TextMessage message = (TextMessage) arg0;

		try {
			String text = message.getText();

			System.out.println("=====msg: - " + text);

		} catch (JMSException e) {
			e.printStackTrace();
		}

	}

}
```