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 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  }