📄 jmsserviceengine.java
字号:
/*
* $Id: JmsServiceEngine.java,v 1.1 2003/08/17 05:12:38 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.service.jms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.XAQueueConnection;
import javax.jms.XAQueueConnectionFactory;
import javax.jms.XAQueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.xa.XAResource;
import org.ofbiz.base.config.GenericConfigException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.JNDIContextFactory;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.serialize.XmlSerializer;
import org.ofbiz.entity.transaction.GenericTransactionException;
import org.ofbiz.entity.transaction.TransactionUtil;
import org.ofbiz.service.GenericRequester;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.service.config.ServiceConfigUtil;
import org.ofbiz.service.engine.GenericEngine;
import org.w3c.dom.Element;
/**
* AbstractJMSEngine
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.1 $
* @since 2.0
*/
public class JmsServiceEngine implements GenericEngine {
public static final String module = JmsServiceEngine.class.getName();
protected ServiceDispatcher dispatcher;
public JmsServiceEngine(ServiceDispatcher dispatcher) {
this.dispatcher = dispatcher;
}
protected Element getServiceElement(ModelService modelService) throws GenericServiceException {
Element rootElement = null;
try {
rootElement = ServiceConfigUtil.getXmlRootElement();
} catch (GenericConfigException e) {
throw new GenericServiceException("Error getting JMS Service element", e);
}
Element serviceElement = UtilXml.firstChildElement(rootElement, "jms-service", "name", modelService.location);
if (serviceElement == null) {
throw new GenericServiceException("Cannot find an JMS service definition for the name [" + modelService.location + "] in the serviceengine.xml file");
}
return serviceElement;
}
protected Message makeMessage(Session session, ModelService modelService, Map context)
throws GenericServiceException, JMSException {
List outParams = modelService.getParameterNames(ModelService.OUT_PARAM, false);
if (outParams != null && outParams.size() > 0)
throw new GenericServiceException("JMS service cannot have required OUT parameters; no parameters will be returned.");
String xmlContext = null;
try {
if (Debug.verboseOn()) Debug.logVerbose("Serializing Context --> " + context, module);
xmlContext = XmlSerializer.serialize(context);
} catch (Exception e) {
throw new GenericServiceException("Cannot serialize context.", e);
}
MapMessage message = session.createMapMessage();
message.setString("serviceName", modelService.invoke);
message.setString("serviceContext", xmlContext);
return message;
}
protected List serverList(Element serviceElement) throws GenericServiceException {
String sendMode = serviceElement.getAttribute("send-mode");
List serverList = UtilXml.childElementList(serviceElement, "server");
if (sendMode.equals("none")) {
return new ArrayList();
} else if (sendMode.equals("all")) {
return serverList;
} else {
throw new GenericServiceException("Requested send mode not supported.");
}
}
protected Map runTopic(ModelService modelService, Map context, Element server) throws GenericServiceException {
String serverName = server.getAttribute("jndi-server-name");
String jndiName = server.getAttribute("jndi-name");
String topicName = server.getAttribute("topic-queue");
String userName = server.getAttribute("username");
String password = server.getAttribute("password");
String clientId = server.getAttribute("client-id");
InitialContext jndi = null;
TopicConnectionFactory factory = null;
TopicConnection con = null;
try {
jndi = JNDIContextFactory.getInitialContext(serverName);
factory = (TopicConnectionFactory) jndi.lookup(jndiName);
} catch (GeneralException ge) {
throw new GenericServiceException("Problems getting JNDI InitialContext.", ge.getNested());
} catch (NamingException ne) {
JNDIContextFactory.clearInitialContext(serverName);
try {
jndi = JNDIContextFactory.getInitialContext(serverName);
factory = (TopicConnectionFactory) jndi.lookup(jndiName);
} catch (GeneralException ge2) {
throw new GenericServiceException("Problems getting JNDI InitialContext.", ge2.getNested());
} catch (NamingException ne2) {
throw new GenericServiceException("JNDI lookup problems.", ne);
}
}
try {
con = factory.createTopicConnection(userName, password);
if (clientId != null && clientId.length() > 1)
con.setClientID(clientId);
con.start();
TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) jndi.lookup(topicName);
TopicPublisher publisher = session.createPublisher(topic);
// create/send the message
Message message = makeMessage(session, modelService, context);
publisher.publish(message);
if (Debug.verboseOn()) Debug.logVerbose("Sent JMS Message to " + topicName, module);
// close the connections
publisher.close();
session.close();
con.close();
} catch (NamingException ne) {
throw new GenericServiceException("Problems with JNDI lookup.", ne);
} catch (JMSException je) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -