Просмотр исходного кода

Made cargo delivery update cause an event to be published and subscribed to (and acted upon), instead of performing checks at the same time the delivery is updated.

Removed unused events from ApplicationEvents.

Removed programmatic JMS adapters in favor of Spring adadpters.
peter_backlund 17 лет назад
Родитель
Сommit
21c9e94193

+ 1
- 16
dddsample/src/main/java/se/citerus/dddsample/application/ApplicationEvents.java Просмотреть файл

@@ -21,24 +21,9 @@ public interface ApplicationEvents {
21 21
 
22 22
   /**
23 23
    * Cargo delivery has been updated.
24
-   * @param cargo cargo
25
-   */
26
-  void cargoDeliveryWasUpdated(Cargo cargo);
27
-
28
-  // TODO remove the two below 
29
-
30
-  /**
31
-   * A cargo has been misdirected.
32 24
    *
33 25
    * @param cargo cargo
34 26
    */
35
-  void cargoWasMisdirected(Cargo cargo);
36
-
37
-  /**
38
-   * A cargo has arrived at its final destination.
39
-   *
40
-   * @param cargo cargo
41
-   */
42
-  void cargoHasArrived(Cargo cargo);
27
+  void cargoDeliveryWasUpdated(Cargo cargo);
43 28
 
44 29
 }

+ 37
- 0
dddsample/src/main/java/se/citerus/dddsample/application/MisdirectedNotifier.java Просмотреть файл

@@ -0,0 +1,37 @@
1
+/**
2
+ * Purpose
3
+ * @author peter
4
+ * @created 2009-aug-03
5
+ * $Id$
6
+ */
7
+package se.citerus.dddsample.application;
8
+
9
+import org.apache.commons.logging.Log;
10
+import org.apache.commons.logging.LogFactory;
11
+import org.springframework.transaction.annotation.Transactional;
12
+import se.citerus.dddsample.domain.model.cargo.Cargo;
13
+import se.citerus.dddsample.domain.model.cargo.CargoRepository;
14
+import se.citerus.dddsample.domain.model.cargo.TrackingId;
15
+
16
+public class MisdirectedNotifier {
17
+
18
+  private CargoRepository cargoRepository;
19
+
20
+  private static final Log LOG = LogFactory.getLog(MisdirectedNotifier.class);
21
+
22
+  public MisdirectedNotifier(final CargoRepository cargoRepository) {
23
+    this.cargoRepository = cargoRepository;
24
+  }
25
+
26
+  @Transactional
27
+  public void alertIfMisdirected(final TrackingId trackingId) {
28
+    final Cargo cargo = cargoRepository.find(trackingId);
29
+
30
+    if (cargo.delivery().isMisdirected()) {
31
+      LOG.info("Cargo " + cargo + " is misdirected!");
32
+    }
33
+  }
34
+
35
+  MisdirectedNotifier() {
36
+  }
37
+}

+ 37
- 0
dddsample/src/main/java/se/citerus/dddsample/application/ReadyToClaimNotfier.java Просмотреть файл

@@ -0,0 +1,37 @@
1
+/**
2
+ * Purpose
3
+ * @author peter
4
+ * @created 2009-aug-03
5
+ * $Id$
6
+ */
7
+package se.citerus.dddsample.application;
8
+
9
+import org.apache.commons.logging.Log;
10
+import org.apache.commons.logging.LogFactory;
11
+import org.springframework.transaction.annotation.Transactional;
12
+import se.citerus.dddsample.domain.model.cargo.Cargo;
13
+import se.citerus.dddsample.domain.model.cargo.CargoRepository;
14
+import se.citerus.dddsample.domain.model.cargo.TrackingId;
15
+
16
+public class ReadyToClaimNotfier {
17
+
18
+  private CargoRepository cargoRepository;
19
+
20
+  private static final Log LOG = LogFactory.getLog(ReadyToClaimNotfier.class);
21
+
22
+  public ReadyToClaimNotfier(final CargoRepository cargoRepository) {
23
+    this.cargoRepository = cargoRepository;
24
+  }
25
+
26
+  @Transactional
27
+  public void alertIfReadyToClaim(final TrackingId trackingId) {
28
+    final Cargo cargo = cargoRepository.find(trackingId);
29
+
30
+    if (cargo.delivery().isUnloadedAtDestination()) {
31
+      LOG.info("Cargo " + cargo + " is ready to be claimed");
32
+    }
33
+  }
34
+
35
+  ReadyToClaimNotfier() {
36
+  }
37
+}

+ 1
- 12
dddsample/src/main/java/se/citerus/dddsample/application/impl/CargoInspectionServiceImpl.java Просмотреть файл

@@ -39,21 +39,10 @@ public class CargoInspectionServiceImpl implements CargoInspectionService {
39 39
     }
40 40
 
41 41
     final HandlingHistory handlingHistory = handlingEventRepository.lookupHandlingHistoryOfCargo(cargo);
42
-
43 42
     cargo.deriveDeliveryProgress(handlingHistory);
44 43
 
45
-    if (cargo.delivery().isMisdirected()) {
46
-      applicationEvents.cargoWasMisdirected(cargo);
47
-    }
48
-
49
-    if (cargo.delivery().isUnloadedAtDestination()) {
50
-      applicationEvents.cargoHasArrived(cargo);
51
-    }
52
-
53 44
     cargoRepository.store(cargo);
54
-
55
-    // TODO replace the inspections above with subscribers to this event
56
-    //applicationEvents.cargoDeliveryWasUpdated(cargo);
45
+    applicationEvents.cargoDeliveryWasUpdated(cargo);
57 46
   }
58 47
 
59 48
 }

+ 0
- 39
dddsample/src/main/java/se/citerus/dddsample/infrastructure/messaging/jms/CargoHandledConsumer.java Просмотреть файл

@@ -1,39 +0,0 @@
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 se.citerus.dddsample.application.CargoInspectionService;
6
-import se.citerus.dddsample.domain.model.cargo.TrackingId;
7
-
8
-import javax.jms.Message;
9
-import javax.jms.MessageListener;
10
-import javax.jms.TextMessage;
11
-
12
-/**
13
- * Consumes JMS messages and delegates notification of misdirected
14
- * cargo to the tracking service.
15
- *
16
- * This is a programmatic hook into the JMS infrastructure to
17
- * make cargo inspection message-driven.
18
- */
19
-public class CargoHandledConsumer implements MessageListener {
20
-
21
-  private CargoInspectionService cargoInspectionService;
22
-  private final Log logger = LogFactory.getLog(getClass());
23
-
24
-  @Override  
25
-  public void onMessage(final Message message) {
26
-    try {
27
-      final TextMessage textMessage = (TextMessage) message;
28
-      final String trackingidString = textMessage.getText();
29
-      
30
-      cargoInspectionService.inspectCargo(new TrackingId(trackingidString));
31
-    } catch (Exception e) {
32
-      logger.error(e, e);
33
-    }
34
-  }
35
-
36
-  public void setCargoInspectionService(CargoInspectionService cargoInspectionService) {
37
-    this.cargoInspectionService = cargoInspectionService;
38
-  }
39
-}

+ 10
- 45
dddsample/src/main/java/se/citerus/dddsample/infrastructure/messaging/jms/JmsApplicationEventsImpl.java Просмотреть файл

@@ -1,7 +1,5 @@
1 1
 package se.citerus.dddsample.infrastructure.messaging.jms;
2 2
 
3
-import org.apache.commons.logging.Log;
4
-import org.apache.commons.logging.LogFactory;
5 3
 import org.springframework.jms.core.JmsOperations;
6 4
 import org.springframework.jms.core.MessageCreator;
7 5
 import se.citerus.dddsample.application.ApplicationEvents;
@@ -19,50 +17,24 @@ import javax.jms.Session;
19 17
 public final class JmsApplicationEventsImpl implements ApplicationEvents {
20 18
 
21 19
   private JmsOperations jmsOperations;
22
-  private Destination cargoHandledQueue;
23
-  private Destination misdirectedCargoQueue;
24
-  private Destination deliveredCargoQueue;
25
-  private Destination cargoDeliveryUpdateQueue;
26
-
27
-  private static final Log logger = LogFactory.getLog(JmsApplicationEventsImpl.class);
20
+  private Destination cargoHandledDestination;
21
+  private Destination cargoDeliveryUpdateDestination;
28 22
 
29 23
   @Override
30 24
   public void cargoWasHandled(final HandlingEvent event) {
31 25
     final Cargo cargo = event.cargo();
32
-    logger.info("Cargo was handled " + cargo);
33
-    jmsOperations.send(cargoHandledQueue, new MessageCreator() {
26
+    jmsOperations.send(cargoHandledDestination, new MessageCreator() {
34 27
       public Message createMessage(final Session session) throws JMSException {
35
-        return session.createTextMessage(cargo.trackingId().stringValue());
28
+        return session.createObjectMessage(cargo.trackingId());
36 29
       }
37 30
     });
38 31
   }
39 32
 
40 33
   @Override
41 34
   public void cargoDeliveryWasUpdated(final Cargo cargo) {
42
-    logger.info("Cargo delivery was updated: " + cargo);
43
-    jmsOperations.send(cargoDeliveryUpdateQueue, new MessageCreator() {
44
-      public Message createMessage(Session session) throws JMSException {
45
-        return session.createTextMessage(cargo.trackingId().stringValue());
46
-      }
47
-    });
48
-  }
49
-
50
-  @Override
51
-  public void cargoWasMisdirected(final Cargo cargo) {
52
-    logger.info("Cargo was misdirected " + cargo);
53
-    jmsOperations.send(misdirectedCargoQueue, new MessageCreator() {
54
-      public Message createMessage(Session session) throws JMSException {
55
-        return session.createTextMessage(cargo.trackingId().stringValue());
56
-      }
57
-    });
58
-  }
59
-
60
-  @Override
61
-  public void cargoHasArrived(final Cargo cargo) {
62
-    logger.info("Cargo has arrived " + cargo);
63
-    jmsOperations.send(deliveredCargoQueue, new MessageCreator() {
35
+    jmsOperations.send(cargoDeliveryUpdateDestination, new MessageCreator() {
64 36
       public Message createMessage(Session session) throws JMSException {
65
-        return session.createTextMessage(cargo.trackingId().stringValue());
37
+        return session.createObjectMessage(cargo.trackingId());
66 38
       }
67 39
     });
68 40
   }
@@ -71,19 +43,12 @@ public final class JmsApplicationEventsImpl implements ApplicationEvents {
71 43
     this.jmsOperations = jmsOperations;
72 44
   }
73 45
 
74
-  public void setCargoHandledQueue(Destination destination) {
75
-    this.cargoHandledQueue = destination;
76
-  }
77
-
78
-  public void setMisdirectedCargoQueue(Destination destination) {
79
-    this.misdirectedCargoQueue = destination;
46
+  public void setCargoHandledDestination(Destination cargoHandledDestination) {
47
+    this.cargoHandledDestination = cargoHandledDestination;
80 48
   }
81 49
 
82
-  public void setDeliveredCargoQueue(Destination destination) {
83
-    this.deliveredCargoQueue = destination;
50
+  public void setCargoDeliveryUpdateDestination(Destination cargoDeliveryUpdateDestination) {
51
+    this.cargoDeliveryUpdateDestination = cargoDeliveryUpdateDestination;
84 52
   }
85 53
 
86
-  public void setCargoDeliveryUpdateQueue(Destination cargoDeliveryUpdateQueue) {
87
-    this.cargoDeliveryUpdateQueue = cargoDeliveryUpdateQueue;
88
-  }
89 54
 }

+ 0
- 18
dddsample/src/main/java/se/citerus/dddsample/infrastructure/messaging/jms/SimpleLoggingConsumer.java Просмотреть файл

@@ -1,18 +0,0 @@
1
-package se.citerus.dddsample.infrastructure.messaging.jms;
2
-
3
-import org.apache.commons.logging.Log;
4
-import org.apache.commons.logging.LogFactory;
5
-
6
-import javax.jms.Message;
7
-import javax.jms.MessageListener;
8
-
9
-public class SimpleLoggingConsumer implements MessageListener {
10
-
11
-  private final Log logger = LogFactory.getLog(SimpleLoggingConsumer.class);
12
-
13
-  @Override
14
-  public void onMessage(Message message) {
15
-    logger.debug("Received JMS message: " + message);
16
-  }
17
-
18
-}

+ 15
- 1
dddsample/src/main/resources/context-application.xml Просмотреть файл

@@ -2,7 +2,13 @@
2 2
 
3 3
 <beans xmlns="http://www.springframework.org/schema/beans"
4 4
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
-       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
5
+       xmlns:context="http://www.springframework.org/schema/context"
6
+       xsi:schemaLocation="
7
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
8
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
9
+
10
+  <!--context:component-scan base-package="se.citerus.dddsample"/>
11
+  <context:annotation-config/-->
6 12
 
7 13
   <bean id="bookingService" class="se.citerus.dddsample.application.impl.BookingServiceImpl">
8 14
     <constructor-arg ref="cargoFactory"/>
@@ -23,4 +29,12 @@
23 29
     <constructor-arg ref="applicationEvents"/>
24 30
   </bean>
25 31
 
32
+  <bean id="misdirectedNotifier" class="se.citerus.dddsample.application.MisdirectedNotifier">
33
+    <constructor-arg ref="cargoRepository"/>
34
+  </bean>
35
+
36
+  <bean id="readyToClaimNotfier" class="se.citerus.dddsample.application.ReadyToClaimNotfier">
37
+    <constructor-arg ref="cargoRepository"/>
38
+  </bean>
39
+
26 40
 </beans>

+ 18
- 14
dddsample/src/main/resources/context-infrastructure-messaging.xml Просмотреть файл

@@ -14,15 +14,12 @@
14 14
   <amq:connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"/>
15 15
 
16 16
   <amq:queue id="cargoHandledQueue" name="CargoHandledQueue" physicalName="CargoHandledQueue"/>
17
-  <amq:queue id="misdirectedCargoQueue" name="MisdirectedCargoQueue" physicalName="MisdirectedCargoQueue"/>
18
-  <amq:queue id="deliveredCargoQueue" name="DeliveredCargoQueue" physicalName="DeliveredCargoQueue"/>
19
-  <amq:queue id="cargoDeliveryUpdateQueue" name="CargoDeliveryUpdateQueue" physicalName="CargoDeliveryUpdateQueue"/>
17
+  <amq:topic id="cargoDeliveryUpdateTopic" name="CargoDeliveryUpdateTopic" physicalName="CargoDeliveryUpdateTopic"/>
20 18
 
21 19
   <jms:listener-container connection-factory="jmsConnectionFactory">
22
-    <jms:listener destination="CargoHandledQueue" ref="cargoHandledConsumer" />
23
-    <jms:listener destination="MisdirectedCargoQueue" ref="simpleLoggingConsumer"/>
24
-    <jms:listener destination="DeliveredCargoQueue" ref="simpleLoggingConsumer"/>
25
-    <jms:listener destination="RejectedRegistrationAttemptsQueue" ref="simpleLoggingConsumer"/>
20
+    <jms:listener destination="CargoHandledQueue" ref="listener3"/>
21
+    <jms:listener destination="CargoDeliveryUpdateTopic" ref="listener1"/>
22
+    <jms:listener destination="CargoDeliveryUpdateTopic" ref="listener2"/>
26 23
   </jms:listener-container>
27 24
 
28 25
   <bean id="jmsOperations" class="org.springframework.jms.core.JmsTemplate">
@@ -31,16 +28,23 @@
31 28
 
32 29
   <bean id="applicationEvents" class="se.citerus.dddsample.infrastructure.messaging.jms.JmsApplicationEventsImpl">
33 30
     <property name="jmsOperations" ref="jmsOperations"/>
34
-    <property name="cargoHandledQueue" ref="cargoHandledQueue"/>
35
-    <property name="misdirectedCargoQueue" ref="misdirectedCargoQueue"/>
36
-    <property name="deliveredCargoQueue" ref="deliveredCargoQueue"/>
37
-    <property name="cargoDeliveryUpdateQueue" ref="cargoDeliveryUpdateQueue"/>
31
+    <property name="cargoHandledDestination" ref="cargoHandledQueue"/>
32
+    <property name="cargoDeliveryUpdateDestination" ref="cargoDeliveryUpdateTopic"/>
38 33
   </bean>
39 34
 
40
-  <bean id="cargoHandledConsumer" class="se.citerus.dddsample.infrastructure.messaging.jms.CargoHandledConsumer">
41
-    <property name="cargoInspectionService" ref="cargoInspectionService"/>
35
+  <bean id="listener1" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
36
+    <constructor-arg ref="misdirectedNotifier"/>
37
+    <property name="defaultListenerMethod" value="alertIfMisdirected"/>
42 38
   </bean>
43 39
 
44
-  <bean id="simpleLoggingConsumer" class="se.citerus.dddsample.infrastructure.messaging.jms.SimpleLoggingConsumer"/>
40
+  <bean id="listener2" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
41
+    <constructor-arg ref="readyToClaimNotfier"/>
42
+    <property name="defaultListenerMethod" value="alertIfReadyToClaim"/>
43
+  </bean>
44
+
45
+  <bean id="listener3" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
46
+    <constructor-arg ref="cargoInspectionService"/>
47
+    <property name="defaultListenerMethod" value="inspectCargo"/>
48
+  </bean>
45 49
 
46 50
 </beans>

+ 0
- 40
dddsample/src/test/java/se/citerus/dddsample/infrastructure/messaging/stub/SynchronousApplicationEventsStub.java Просмотреть файл

@@ -1,40 +0,0 @@
1
-package se.citerus.dddsample.infrastructure.messaging.stub;
2
-
3
-import org.apache.commons.logging.Log;
4
-import org.apache.commons.logging.LogFactory;
5
-import se.citerus.dddsample.application.ApplicationEvents;
6
-import se.citerus.dddsample.application.CargoInspectionService;
7
-import se.citerus.dddsample.domain.model.cargo.Cargo;
8
-import se.citerus.dddsample.domain.model.handling.HandlingEvent;
9
-
10
-public class SynchronousApplicationEventsStub implements ApplicationEvents {
11
-
12
-  CargoInspectionService cargoInspectionService;
13
-  private static final Log logger = LogFactory.getLog(SynchronousApplicationEventsStub.class);
14
-
15
-  public void setCargoInspectionService(CargoInspectionService cargoInspectionService) {
16
-    this.cargoInspectionService = cargoInspectionService;
17
-  }
18
-
19
-  @Override
20
-  public void cargoWasHandled(HandlingEvent event) {
21
-    logger.debug("EVENT: cargo was handled: " + event);
22
-    cargoInspectionService.inspectCargo(event.cargo().trackingId());
23
-  }
24
-
25
-  @Override
26
-  public void cargoDeliveryWasUpdated(Cargo cargo) {
27
-    logger.debug("EVENT: cargo delivery was updated");
28
-  }
29
-
30
-  @Override
31
-  public void cargoWasMisdirected(Cargo cargo) {
32
-    logger.debug("EVENT: cargo was misdirected");
33
-  }
34
-
35
-  @Override
36
-  public void cargoHasArrived(Cargo cargo) {
37
-    logger.debug("EVENT: cargo has arrived: " + cargo.trackingId().stringValue());
38
-  }
39
-
40
-}