AtmiBrokerServer.java
上传用户:xfwatch
上传日期:2020-12-14
资源大小:872k
文件大小:6k
源码类别:

中间件编程

开发平台:

Java

  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2008, Red Hat, Inc., and others contributors as indicated
  4.  * by the @authors tag. All rights reserved.
  5.  * See the copyright.txt in the distribution for a
  6.  * full listing of individual contributors.
  7.  * This copyrighted material is made available to anyone wishing to use,
  8.  * modify, copy, or redistribute it subject to the terms and conditions
  9.  * of the GNU Lesser General Public License, v. 2.1.
  10.  * This program is distributed in the hope that it will be useful, but WITHOUT A
  11.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12.  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  13.  * You should have received a copy of the GNU Lesser General Public License,
  14.  * v.2.1 along with this distribution; if not, write to the Free Software
  15.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16.  * MA  02110-1301, USA.
  17.  */
  18. package org.jboss.blacktie.jatmibroker.core.server;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;
  25. import java.util.StringTokenizer;
  26. import org.apache.log4j.LogManager;
  27. import org.apache.log4j.Logger;
  28. import org.jboss.blacktie.jatmibroker.core.conf.AtmiBrokerServerXML;
  29. import org.jboss.blacktie.jatmibroker.core.conf.ConfigurationException;
  30. import org.jboss.blacktie.jatmibroker.core.transport.OrbManagement;
  31. import org.jboss.blacktie.jatmibroker.core.transport.Receiver;
  32. import org.jboss.blacktie.jatmibroker.core.transport.Transport;
  33. import org.jboss.blacktie.jatmibroker.core.transport.TransportFactory;
  34. import org.jboss.blacktie.jatmibroker.xatmi.BlacktieService;
  35. import org.jboss.blacktie.jatmibroker.xatmi.Connection;
  36. import org.jboss.blacktie.jatmibroker.xatmi.ConnectionException;
  37. public class AtmiBrokerServer {
  38. private static final Logger log = LogManager
  39. .getLogger(AtmiBrokerServer.class);
  40. private Map<String, ServiceData> serviceData = new HashMap<String, ServiceData>();
  41. private OrbManagement orbManagement;
  42. private Properties properties;
  43. private String serverName;
  44. private static final String DEFAULT_POOL_SIZE = "1";
  45. public AtmiBrokerServer(String serverName) throws ConfigurationException,
  46. ConnectionException {
  47. this.serverName = serverName;
  48. AtmiBrokerServerXML server = new AtmiBrokerServerXML();
  49. properties = server.getProperties();
  50. try {
  51. orbManagement = new OrbManagement(properties, true);
  52. } catch (Throwable t) {
  53. throw new ConnectionException(-1, "Could not connect to orb", t);
  54. }
  55. String services = (String) properties.get("blacktie." + serverName
  56. + ".services");
  57. if (services != null) {
  58. StringTokenizer st = new StringTokenizer(services, ",", false);
  59. while (st.hasMoreElements()) {
  60. String serviceName = st.nextToken();
  61. String functionName = (String) properties.get("blacktie."
  62. + serviceName + ".java_class_name");
  63. tpadvertise(serviceName, functionName);
  64. }
  65. }
  66. }
  67. public void close() throws ConnectionException {
  68. log.debug("Close server called: " + serverName);
  69. Iterator<String> names = serviceData.keySet().iterator();
  70. while (names.hasNext()) {
  71. ServiceData next = serviceData.get(names.next());
  72. next.close();
  73. names.remove();
  74. }
  75. log.debug("Close server finished: " + serverName);
  76. }
  77. /**
  78.  * Create a blacktie service with the specified name
  79.  * 
  80.  * @param serviceName
  81.  *            The name of the service
  82.  * @throws ConnectionException
  83.  *             If the service cannot be advertised
  84.  */
  85. public void tpadvertise(String serviceName, String serviceClassName)
  86. throws ConnectionException {
  87. int min = Math.min(Connection.XATMI_SERVICE_NAME_LENGTH, serviceName
  88. .length());
  89. serviceName = serviceName.substring(0, min);
  90. try {
  91. log.debug("Advertising: " + serviceName);
  92. if (!serviceData.containsKey(serviceName)) {
  93. try {
  94. ServiceData data = new ServiceData(serviceName,
  95. serviceClassName);
  96. serviceData.put(serviceName, data);
  97. log.info("Advertised: " + serviceName);
  98. } catch (Throwable t) {
  99. throw new ConnectionException(-1,
  100. "Could not create service factory for: "
  101. + serviceName, t);
  102. }
  103. } else {
  104. throw new ConnectionException(Connection.TPEMATCH,
  105. "Service already registered");
  106. }
  107. } catch (Throwable t) {
  108. String message = "Could not advertise: " + serviceName;
  109. log.error(message, t);
  110. throw new ConnectionException(-1, message, t);
  111. }
  112. }
  113. public void tpunadvertise(String serviceName) throws ConnectionException {
  114. serviceName = serviceName.substring(0, Math.min(
  115. Connection.XATMI_SERVICE_NAME_LENGTH, serviceName.length()));
  116. ServiceData data = serviceData.remove(serviceName);
  117. if (data != null) {
  118. try {
  119. data.close();
  120. } catch (Throwable t) {
  121. log.error("Could not unadvertise: " + serviceName, t);
  122. }
  123. }
  124. }
  125. private class ServiceData {
  126. private Receiver receiver;
  127. private List<ServiceDispatcher> dispatchers = new ArrayList<ServiceDispatcher>();
  128. private Transport connection;
  129. private String serviceName;
  130. ServiceData(String serviceName, String serviceClassName)
  131. throws ConnectionException, InstantiationException,
  132. IllegalAccessException, ClassNotFoundException,
  133. ConfigurationException {
  134. this.serviceName = serviceName;
  135. String sizeS = properties.getProperty("blacktie." + serviceName
  136. + ".size", DEFAULT_POOL_SIZE);
  137. int size = Integer.parseInt(sizeS);
  138. connection = TransportFactory.loadTransportFactory(serviceName,
  139. properties).createTransport();
  140. this.receiver = connection.getReceiver(serviceName);
  141. Class callback = Class.forName(serviceClassName);
  142. for (int i = 0; i < size; i++) {
  143. dispatchers.add(new ServiceDispatcher(serviceName,
  144. (BlacktieService) callback.newInstance(), receiver));
  145. }
  146. }
  147. public void close() throws ConnectionException {
  148. log.debug("Unadvertising: " + serviceName);
  149. // Clean up the consumers
  150. Iterator<ServiceDispatcher> iterator = dispatchers.iterator();
  151. while (iterator.hasNext()) {
  152. iterator.next().startClose();
  153. }
  154. // Disconnect the receiver
  155. receiver.close();
  156. // Disconnect the transport
  157. connection.close();
  158. // Clean up the consumers
  159. iterator = dispatchers.iterator();
  160. while (iterator.hasNext()) {
  161. iterator.next().close();
  162. }
  163. dispatchers.clear();
  164. log.info("Unadvertised: " + serviceName);
  165. }
  166. }
  167. }