使用 Apache Geronimo 和 JMS 构建事件驱动的框架
2010-04-23 00:00:00 来源:WEB开发网清单 4 显示了事件通道的一个摘录。
清单 4. 事件通道的实现
public class EventChannel
implements Channel
{
private static final String TOPIC_NAME =
"java:comp/env/EventTopic";
private static final String MQ_URL = "tcp://localhost:61616";
private HashMap subscribers = new HashMap();
private TopicConnectionFactory factory = null;
private Topic eventTopic = null;
private TopicConnection topicConn = null;
private TopicSession topicSess = null;
private TopicSubscriber topicSubscriber = null;
private TopicPublisher topicPublisher = null;
private EventConsumer eventConsumer = null;
private void handleEvent(Event event)
{
final Set received = new HashSet();
for (Class eventClass = event.getClass();
Event.class.isAssignableFrom(eventClass);
eventClass = eventClass.getSuperclass())
{
ArrayList receiverList = new ArrayList();
getReceiversForEvent(getEventLeafInterface(eventClass),
receiverList);
Receiver[] receivers = new Receiver[receiverList.size()];
receiverList.toArray(receivers);
for (int i = 0; i < receivers.length; i++)
{
invokeOnce(received, receivers[i], event);
}
}
}
private void invokeOnce(Set received,
Receiver receiver,
Event event)
{
received.add(receiver);
receiver.receive(event);
}
private Class getEventLeafInterface(Class cls)
{
Class retVal = null;
if (Event.class.isAssignableFrom(cls))
{
retVal = cls;
if (cls.isInterface())
{
return retVal;
}
}
Class[] interfaces = cls.getInterfaces();
if (interfaces != null)
{
for (int i = 0; i < interfaces.length; i++)
{
if (Event.class.isAssignableFrom(interfaces[i]))
{
retVal = interfaces[i];
break;
}
retVal = getEventLeafInterface(interfaces[i]);
}
}
return retVal;
}
public void start()
{
try
{
factory = new ActiveMQConnectionFactory(MQ_URL);
topicConn = factory.createTopicConnection();
topicSess =
topicConn.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
eventTopic = topicSess.createTopic(TOPIC_NAME);
topicSubscriber = topicSess.createSubscriber(eventTopic);
topicPublisher = topicSess.createPublisher(eventTopic);
eventConsumer = new EventConsumer(this);
Thread consumerThread = new Thread(eventConsumer);
consumerThread.setDaemon(false);
consumerThread.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void stop()
{
// close topic connections, sessions, consumers, etc.
}
public void publish(final Event event)
{
try
{
ObjectMessage eventMessage = topicSess.createObjectMessage();
eventMessage.setObject(event);
topicPublisher.publish(eventMessage);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void subscribe(final Receiver receiver,
final Class eventClass)
{
ArrayList receiverList = null;
Class leafCls = getEventLeafInterface(eventClass);
if (subscribers.get(leafCls) == null)
{
receiverList = new ArrayList();
subscribers.put(leafCls, receiverList);
}
else
{
receiverList = (ArrayList) subscribers.get(leafCls);
}
if (receiverList.indexOf(receiver) < 0)
{
receiverList.add(receiver);
}
}
public void unsubscribe(final Receiver receiver,
final Class eventClass)
{
Class leafCls = getEventLeafInterface(eventClass);
if (subscribers.get(leafCls) != null)
{
ArrayList receiverList = (ArrayList) subscribers.get(leafCls);
receiverList.remove(receiverList);
}
}
}
- ››使用linux中的quota教程
- ››apache设置域名绑定 以及绑定不起作用的排查
- ››使用jxl生成带动态折线图的excel
- ››apache rewrite将指定URL转向指定的几个服务器
- ››使用mysql mysqldump进行数据库迁移
- ››使用jquery是新tab形式
- ››使用QUnit进行Javascript单元测试
- ››使用UITextFieldDelegate来隐藏键盘
- ››使用公式提取Excel中的日期后发现格式不对
- ››使用SQL Azure 的BI 解决方案
- ››使用PLSQL Developer工具导出sql文件
- ››使用双缓冲技术实现Android画板应用
更多精彩
赞助商链接