WEB开发网
开发学院软件开发Java 使用 Apache Geronimo 和 JMS 构建事件驱动的框架... 阅读

使用 Apache Geronimo 和 JMS 构建事件驱动的框架

 2010-04-23 00:00:00 来源:WEB开发网   
核心提示: 清单 4 显示了事件通道的一个摘录。清单 4. 事件通道的实现publicclassEventChannelimplementsChannel{privatestaticfinalStringTOPIC_NAME="java:comp/env/EventTopic";priv

清单 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); 
     } 
    } 
   } 

上一页  2 3 4 5 6 7 8 9 10  下一页

Tags:使用 Apache Geronimo

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接