📄 basictopicsubscriber.java
字号:
package com.wrox.pubsub;
import javax.naming.*;
import javax.jms.*;
public class BasicTopicSubscriber
{
protected TopicConnection _connection =null;
protected TopicSession _session = null;
protected TopicSubscriber _subscriber = null;
protected String _jndiTopicName = null;
protected String _jndiTopicFactoryName = null;
public BasicTopicSubscriber (String jndiTopicFactoryName, String jndiTopicName) throws NamingException, JMSException
{
super();
_jndiTopicName = jndiTopicName;
_jndiTopicFactoryName = jndiTopicFactoryName;
initialize ();
}
protected void initialize () throws NamingException, JMSException
{
Context ctx = new InitialContext ();
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory) ctx.lookup (_jndiTopicFactoryName);
Topic topic = (Topic) ctx.lookup (_jndiTopicName);
_connection = topicConnectionFactory.createTopicConnection ();
_session = _connection.createTopicSession (false, Session.AUTO_ACKNOWLEDGE);
_subscriber = _session.createSubscriber (topic);
}
public void subscribe(MessageListener messageListener) throws JMSException
{
_connection.stop ();
_subscriber.setMessageListener (messageListener);
_connection.start ();
}
public void finalize ()
{
try
{
cleanup ();
}
catch(JMSException jmsEx)
{
// ignore this error
}
}
public void cleanup () throws JMSException
{
if(_connection != null)
{
_connection.close ();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -