01: package com.mockrunner.example.jms;
02:
03: import java.rmi.RemoteException;
04:
05: import javax.ejb.CreateException;
06: import javax.ejb.EJBException;
07: import javax.ejb.SessionBean;
08: import javax.ejb.SessionContext;
09: import javax.jms.JMSException;
10: import javax.jms.Queue;
11: import javax.jms.QueueConnection;
12: import javax.jms.QueueConnectionFactory;
13: import javax.jms.QueueSender;
14: import javax.jms.QueueSession;
15: import javax.jms.Session;
16: import javax.jms.TextMessage;
17: import javax.naming.InitialContext;
18:
19: /*
20: * @ejb:bean name="PrintSession"
21: * display-name="PrintSessionBean"
22: * type="Stateless"
23: * transaction-type="Container"
24: * jndi-name="com/mockrunner/example/PrintSession"
25: */
26: /**
27: * This is the EJB version of {@link PrintMessageServlet}.
28: * The receiver is {@link PrintMessageDrivenBean}.
29: */
30: public class PrintSessionBean implements SessionBean {
31: private SessionContext sessionContext;
32:
33: /*
34: * @ejb:interface-method
35: * @ejb:transaction type="Required"
36: */
37: public void sendMessage(String customerId) {
38: QueueConnection queueConnection = null;
39: QueueSession queueSession = null;
40: QueueSender queueSender = null;
41: try {
42: InitialContext initialContext = new InitialContext();
43: QueueConnectionFactory queueFactory = (QueueConnectionFactory) initialContext
44: .lookup("java:/ConnectionFactory");
45: queueConnection = queueFactory.createQueueConnection();
46: queueSession = queueConnection.createQueueSession(true,
47: Session.CLIENT_ACKNOWLEDGE);
48: Queue queue = (Queue) initialContext
49: .lookup("queue/testQueue");
50: TextMessage message = queueSession
51: .createTextMessage(customerId);
52: queueSender = queueSession.createSender(queue);
53: queueSender.send(message);
54: } catch (Exception exc) {
55: sessionContext.setRollbackOnly();
56: } finally {
57: try {
58: if (null != queueSender)
59: queueSender.close();
60: if (null != queueSession)
61: queueSession.close();
62: if (null != queueConnection)
63: queueConnection.close();
64: } catch (JMSException exc) {
65: exc.printStackTrace();
66: }
67: }
68: }
69:
70: /*
71: * @ejb:create-method
72: */
73: public void ejbCreate() throws CreateException {
74:
75: }
76:
77: public void ejbActivate() throws EJBException, RemoteException {
78:
79: }
80:
81: public void ejbPassivate() throws EJBException, RemoteException {
82:
83: }
84:
85: public void ejbRemove() throws EJBException, RemoteException {
86:
87: }
88:
89: public void setSessionContext(SessionContext sessionContext)
90: throws EJBException, RemoteException {
91: this.sessionContext = sessionContext;
92: }
93: }
|