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