View Javadoc

1   package se.citerus.dddsample.infrastructure.messaging.jms;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.springframework.jms.core.JmsOperations;
6   import org.springframework.jms.core.MessageCreator;
7   import se.citerus.dddsample.application.ApplicationEvents;
8   import se.citerus.dddsample.domain.model.cargo.Cargo;
9   import se.citerus.dddsample.domain.model.handling.HandlingEvent;
10  import se.citerus.dddsample.interfaces.handling.HandlingEventRegistrationAttempt;
11  
12  import javax.jms.Destination;
13  import javax.jms.JMSException;
14  import javax.jms.Message;
15  import javax.jms.Session;
16  
17  /**
18   * JMS based implementation.
19   */
20  public final class JmsApplicationEventsImpl implements ApplicationEvents {
21  
22    private JmsOperations jmsOperations;
23    private Destination cargoHandledQueue;
24    private Destination misdirectedCargoQueue;
25    private Destination deliveredCargoQueue;
26    private Destination rejectedRegistrationAttemptsQueue;
27    private Destination handlingEventQueue;
28  
29    private static final Log logger = LogFactory.getLog(JmsApplicationEventsImpl.class);
30  
31    @Override
32    public void cargoWasHandled(final HandlingEvent event) {
33      final Cargo cargo = event.cargo();
34      logger.info("Cargo was handled " + cargo);
35      jmsOperations.send(cargoHandledQueue, new MessageCreator() {
36        public Message createMessage(final Session session) throws JMSException {
37          return session.createTextMessage(cargo.trackingId().idString());
38        }
39      });
40    }
41  
42    @Override
43    public void cargoWasMisdirected(final Cargo cargo) {
44      logger.info("Cargo was misdirected " + cargo);
45      jmsOperations.send(misdirectedCargoQueue, new MessageCreator() {
46        public Message createMessage(Session session) throws JMSException {
47          return session.createTextMessage(cargo.trackingId().idString());
48        }
49      });
50    }
51  
52    @Override
53    public void cargoHasArrived(final Cargo cargo) {
54      logger.info("Cargo has arrived " + cargo);
55      jmsOperations.send(deliveredCargoQueue, new MessageCreator() {
56        public Message createMessage(Session session) throws JMSException {
57          return session.createTextMessage(cargo.trackingId().idString());
58        }
59      });
60    }
61  
62    @Override
63    public void receivedHandlingEventRegistrationAttempt(final HandlingEventRegistrationAttempt attempt) {
64      logger.info("Received handling event registration attempt " + attempt);
65      jmsOperations.send(handlingEventQueue, new MessageCreator() {
66        public Message createMessage(Session session) throws JMSException {
67          return session.createObjectMessage(attempt);
68        }
69      });
70    }
71  
72    public void setJmsOperations(JmsOperations jmsOperations) {
73      this.jmsOperations = jmsOperations;
74    }
75  
76    public void setCargoHandledQueue(Destination destination) {
77      this.cargoHandledQueue = destination;
78    }
79  
80    public void setMisdirectedCargoQueue(Destination destination) {
81      this.misdirectedCargoQueue = destination;
82    }
83  
84    public void setDeliveredCargoQueue(Destination destination) {
85      this.deliveredCargoQueue = destination;
86    }
87  
88    public void setRejectedRegistrationAttemptsQueue(Destination destination) {
89      this.rejectedRegistrationAttemptsQueue = destination;
90    }
91  
92    public void setHandlingEventQueue(Destination destination) {
93      this.handlingEventQueue = destination;
94    }
95  }