CalculatorMDB.java
01 package trail.injection;
02 
03 import javax.ejb.*;
04 import javax.jms.*;
05 import java.util.StringTokenizer;
06 import trail.slsb.*;
07 
08 @MessageDriven(activationConfig =
09 {
10   @ActivationConfigProperty(propertyName="destinationType",
11     propertyValue="javax.jms.Queue"),
12   @ActivationConfigProperty(propertyName="destination",
13     propertyValue="queue/injection")
14 })
15 public class CalculatorMDB implements MessageListener {
16 
17   // Inject EJBs
18   @EJB (beanName="StatelessCalculator")
19   Calculator cal;
20 
21   // @EJB (beanName="RecordManagerBean")
22   @EJB
23   RecordManager rm;
24 
25   public void onMessage (Message msg) {
26     try {
27       TextMessage tmsg = (TextMessagemsg;
28       long sent = tmsg.getLongProperty("sent");
29       StringTokenizer st =
30           new StringTokenizer(tmsg.getText()",");
31 
32       int start = Integer.parseInt(st.nextToken());
33       int end = Integer.parseInt(st.nextToken());
34       double growthrate = Double.parseDouble(st.nextToken());
35       double saving = Double.parseDouble(st.nextToken());
36 
37       // Calculate it with the injected stateless session bean
38       double result =
39           cal.calculate (start, end, growthrate, saving);
40 
41       /*
42 
43       // Calculate it again with the MDB at queue/mdb
44       QueueConnection cnn = factory.createQueueConnection();
45       QueueSession sess = cnn.createQueueSession(false,
46                          QueueSession.AUTO_ACKNOWLEDGE);
47       QueueSender sender = sess.createSender(queue);
48       sender.send(msg);
49       // sess.commit ();
50       sess.close ();
51       // Wait for the MDB to respond
52       Thread.sleep (2000);
53 
54       // Compare the two results!
55       boolean verify = (result ==
56           trail.mdb.RecordManager.getRecord(sent.getTime()).result);
57 
58       // Get timers that are associated with this EJB
59       int timers = tms.getTimers().size();
60 
61       */
62 
63       rm.addRecord (sent, result);
64 
65       System.out.println ("The onMessage() is called");
66 
67     catch (Exception e) {
68       e.printStackTrace ();
69     }
70   }
71 
72 }