KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > messager > MessagerService


1 package org.jbpm.bpel.messager;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import javax.jms.Connection JavaDoc;
8 import javax.jms.ConnectionFactory JavaDoc;
9 import javax.jms.JMSException JavaDoc;
10 import javax.jms.Session JavaDoc;
11 import javax.naming.Context JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.jbpm.db.JbpmSession;
17 import org.jbpm.db.JbpmSessionFactory;
18
19 import org.jbpm.bpel.bar.BarApplication;
20 import org.jbpm.bpel.def.BpelDefinition;
21 import org.jbpm.bpel.par.JndiProcessDeployer;
22 import org.jbpm.bpel.service.def.PartnerLinkDefinition;
23 import org.jbpm.bpel.service.exe.StartListener;
24
25 /**
26  * @author Alejandro Guízar
27  * @version $Revision: 1.8 $ $Date: 2005/06/04 00:08:20 $
28  */

29 public class MessagerService {
30   
31   private final BpelDefinition process;
32   private final ConnectionFactory JavaDoc connectionFactory;
33   private final Map JavaDoc partnerLinkInfos;
34   private JbpmSessionFactory jbpmSessionFactory;
35   
36   private Connection JavaDoc connection;
37   private List JavaDoc startListeners;
38   
39   private static final Log log = LogFactory.getLog(MessagerService.class);
40   
41   public MessagerService(BpelDefinition process, ConnectionFactory JavaDoc connectionFactory, Map JavaDoc partnerLinkInfos) {
42     // save parameters
43
this.process = process;
44     this.connectionFactory = connectionFactory;
45     this.partnerLinkInfos = partnerLinkInfos;
46     // create a connection
47
try {
48       connection = connectionFactory.createConnection();
49     }
50     catch (JMSException JavaDoc e) {
51       log.error(e);
52       throw new RuntimeException JavaDoc("could not create jms connection", e);
53     }
54   }
55   
56   public BpelDefinition getProcess() {
57     return process;
58   }
59
60   public ConnectionFactory JavaDoc getConnectionFactory() {
61     return connectionFactory;
62   }
63
64   public Connection JavaDoc getConnection() {
65     return connection;
66   }
67   
68   public MessagerSession openSession() {
69     try {
70       Session JavaDoc jmsSession = connection.createSession(true, Session.SESSION_TRANSACTED);
71       MessagerSession msgSession = new MessagerSession(jmsSession, this);
72       if (jbpmSessionFactory != null) {
73         try {
74           JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
75           msgSession.setJbpmSession(jbpmSession);
76         }
77         catch (RuntimeException JavaDoc e) {
78           jmsSession.close();
79           throw e;
80         }
81       }
82       return msgSession;
83     }
84     catch (JMSException JavaDoc e) {
85       log.error(e);
86       throw new RuntimeException JavaDoc("could not create jms session", e);
87     }
88   }
89   
90   public PartnerLinkInfo getPartnerLinkInfo(PartnerLinkDefinition partnerLink) {
91     return (PartnerLinkInfo) partnerLinkInfos.get(new Long JavaDoc(partnerLink.getId()));
92   }
93   
94   public JbpmSessionFactory getJbpmSessionFactory() {
95     return jbpmSessionFactory;
96   }
97   
98   public void setJbpmSessionFactory(JbpmSessionFactory jbpmSessionFactory) {
99     this.jbpmSessionFactory = jbpmSessionFactory;
100   }
101   
102   /**
103    * Prepares the service for receiving messages that request the creation of
104    * a process instance, and starts message delivery.
105    */

106   public void start() {
107     // open request listeners
108
StartListenersActivator activator = new StartListenersActivator(openSession());
109     activator.visit(process);
110     startListeners = activator.getStartListeners();
111     // start message delivery
112
try {
113       connection.start();
114     }
115     catch (JMSException JavaDoc e) {
116       log.error(e);
117       throw new RuntimeException JavaDoc("could not start connection", e);
118     }
119   }
120   
121   /**
122    * Stops message delivery and cancels receptions of messages that request the
123    * creation of a process instance.
124    */

125   public void stop() {
126     // stop message delivery
127
try {
128       connection.stop();
129     }
130     catch (JMSException JavaDoc e) {
131       log.warn("could not stop connection", e);
132     }
133     // close request listeners
134
for (int i = 0, n = startListeners.size(); i < n; i++) {
135       StartListener requestListener = (StartListener) startListeners.get(i);
136       try {
137         requestListener.close();
138       }
139       catch (RuntimeException JavaDoc e) {
140         // ignore, already logged within the operation
141
}
142     }
143   }
144   
145   public void destroy(Context JavaDoc namingContext) {
146     // close connection
147
try {
148       connection.close();
149     }
150     catch (JMSException JavaDoc e) {
151       log.warn("could not close jms connection", e);
152     }
153     connection = null;
154     // retrieve process context
155
JndiProcessDeployer deployer = new JndiProcessDeployer(namingContext);
156     String JavaDoc processName = process.getName();
157     // unbind endpoint infos
158
Iterator JavaDoc plinkInfoIt = partnerLinkInfos.values().iterator();
159     while (plinkInfoIt.hasNext()) {
160       PartnerLinkInfo plinkInfo = (PartnerLinkInfo) plinkInfoIt.next();
161       String JavaDoc endpointInfoName = plinkInfo.getEndpointInfoName();
162       try {
163         deployer.undeployEndpointInfo(processName, endpointInfoName);
164       }
165       catch (RuntimeException JavaDoc e) {
166         // ignore, already logged within the operation
167
}
168     }
169   }
170   
171   public static MessagerService buildMessagerService(BarApplication application,
172       BpelDefinition process, Context JavaDoc namingContext) {
173     return new MessagerServiceBuilder(namingContext).buildMessagerService(application, process);
174   }
175 }
176
Popular Tags