Announcement Announcement Module
Collapse
No announcement yet.
Is there a spring equivalent to the EJB Observer Pattern? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there a spring equivalent to the EJB Observer Pattern?

    I basically would like that a service method fire events related to what was inserted, updated or deleted in the DAO layer and any other arbritrary events I might fire explicitly in my service layer. These events should somehow be available in my caller. So the code would be like

    Caller:

    serviceInstance.addListener(eventListener);
    serviceInstance.doSomething();
    if (eventListener.isEvent1Fired()) {
    ...
    }

    } finally {
    serviceInstance.removeListener(eventListener);
    }


    ServiceInstance:
    void doSomeThing() {
    fireEvent1();
    }


    Haven't specified in full the interface. But hopefully one of you will get the idea and let me know if there is a similar thing is Spring that is available for doing this sort of thing.

    Thanks,
    Garry

    EJB Observer Pattern
    This article explains how to implement the observer pattern in a suite of integrated EJB apps. The suite has a shared repository and each EJB application has it own repository. Every application in the suite runs on a separate host. Each application needs to observe changes in the shared repository as they occur -- and within the transaction in which they occur. If a proposed change to the shared data is unacceptable to any given application then the transaction must be aborted before the change is made permanent.

  • #2
    You should be able to use a combination of Spring AOP (to capture and fire events without code duplication) and the event publication method oof a Spring ApplicationContext to do this. See EventPublicationInterceptor as a starting point.

    Without a URL, it's hard to comment on the article. It sounds like it's not EJB specific. If you need to propagate events around a cluster, you should look at JMS. You could have a listener that fires Spring application context events to a JMS queue.

    Comment


    • #3
      I looked for an example but I can't find any.

      I looked for an example but I can't find any. Could you provide an example of how best to accomplish this in a Spring way?

      Basically what I want to accomplish is the best pattern for the following:

      1) Struts Action-> calls Spring Transaction Proxied Service Method
      2) need feedback in struts action about events (successes and errors) that occured in Service Method. Also the service could have derived a value or multiple values which need to be passed back to the client in request attributes.

      For instance

      struts action:
      service method:
      Listener quantityNotificationListener = new QuantityNotificationListener();
      getWebApplicationContext()
      .registerNotificationListener(quantityNotification Listener);
      Listener eventListener = new EventListener
      getWebApplicationContext()
      .registerEventListener(eventListener);

      processOrder(orderId,orderName,orderQuantity);
      // now some through WebApplicationContext
      // action is aware of fired events and fired notifications i.e:
      request.setAttribute("NEW_QUANTITY", quantityNotificationListener.getNotificationObject ());
      addMessages to ActionMessages based on events fired

      service method:
      processOrder(orderId,orderQuantity) {
      try {
      Order order = getOrderFromSupply(orderId,orderQuantity);
      } catch (OrderNotAvailableException e) {
      fireEvent(new OrderNotAvailableEvent());
      try {
      savebackOrder(orderId,orderQuantity);
      } catch (OrderDiscontinuedException e) {
      fireEvent(new OrderDiscontinuedEvent());
      }
      completeOrder(order);
      fireNotify(new QuantityNotification());
      fireEvent(new OrderCompletedEvent());
      }



      }

      Comment


      • #4
        You'd asked article about the Publisher-Observer wrt EJB

        I think the following is a pretty good article to illustrate the programming issues I'm trying to address.

        http://www.javaworld.com/javaworld/j...avatip110.html

        Comment

        Working...
        X