1 package org.jbpm.bpel.messager; 2 3 import java.util.Iterator ; 4 import java.util.List ; 5 import java.util.Map ; 6 7 import javax.jms.Connection ; 8 import javax.jms.ConnectionFactory ; 9 import javax.jms.JMSException ; 10 import javax.jms.Session ; 11 import javax.naming.Context ; 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 29 public class MessagerService { 30 31 private final BpelDefinition process; 32 private final ConnectionFactory connectionFactory; 33 private final Map partnerLinkInfos; 34 private JbpmSessionFactory jbpmSessionFactory; 35 36 private Connection connection; 37 private List startListeners; 38 39 private static final Log log = LogFactory.getLog(MessagerService.class); 40 41 public MessagerService(BpelDefinition process, ConnectionFactory connectionFactory, Map partnerLinkInfos) { 42 this.process = process; 44 this.connectionFactory = connectionFactory; 45 this.partnerLinkInfos = partnerLinkInfos; 46 try { 48 connection = connectionFactory.createConnection(); 49 } 50 catch (JMSException e) { 51 log.error(e); 52 throw new RuntimeException ("could not create jms connection", e); 53 } 54 } 55 56 public BpelDefinition getProcess() { 57 return process; 58 } 59 60 public ConnectionFactory getConnectionFactory() { 61 return connectionFactory; 62 } 63 64 public Connection getConnection() { 65 return connection; 66 } 67 68 public MessagerSession openSession() { 69 try { 70 Session 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 e) { 78 jmsSession.close(); 79 throw e; 80 } 81 } 82 return msgSession; 83 } 84 catch (JMSException e) { 85 log.error(e); 86 throw new RuntimeException ("could not create jms session", e); 87 } 88 } 89 90 public PartnerLinkInfo getPartnerLinkInfo(PartnerLinkDefinition partnerLink) { 91 return (PartnerLinkInfo) partnerLinkInfos.get(new Long (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 106 public void start() { 107 StartListenersActivator activator = new StartListenersActivator(openSession()); 109 activator.visit(process); 110 startListeners = activator.getStartListeners(); 111 try { 113 connection.start(); 114 } 115 catch (JMSException e) { 116 log.error(e); 117 throw new RuntimeException ("could not start connection", e); 118 } 119 } 120 121 125 public void stop() { 126 try { 128 connection.stop(); 129 } 130 catch (JMSException e) { 131 log.warn("could not stop connection", e); 132 } 133 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 e) { 140 } 142 } 143 } 144 145 public void destroy(Context namingContext) { 146 try { 148 connection.close(); 149 } 150 catch (JMSException e) { 151 log.warn("could not close jms connection", e); 152 } 153 connection = null; 154 JndiProcessDeployer deployer = new JndiProcessDeployer(namingContext); 156 String processName = process.getName(); 157 Iterator plinkInfoIt = partnerLinkInfos.values().iterator(); 159 while (plinkInfoIt.hasNext()) { 160 PartnerLinkInfo plinkInfo = (PartnerLinkInfo) plinkInfoIt.next(); 161 String endpointInfoName = plinkInfo.getEndpointInfoName(); 162 try { 163 deployer.undeployEndpointInfo(processName, endpointInfoName); 164 } 165 catch (RuntimeException e) { 166 } 168 } 169 } 170 171 public static MessagerService buildMessagerService(BarApplication application, 172 BpelDefinition process, Context namingContext) { 173 return new MessagerServiceBuilder(namingContext).buildMessagerService(application, process); 174 } 175 } 176 | Popular Tags |