Changeset 37
- Timestamp:
- 06/12/13 17:02:52 (11 years ago)
- Location:
- trunk/objectmq
- Files:
-
- 8 added
- 1 deleted
- 14 edited
- 2 copied
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/objectmq/src/omq/common/broker/Broker.java
r36 r37 2 2 3 3 import java.io.IOException; 4 import java.util.HashMap; 5 import java.util.Map; 4 6 import java.util.Properties; 5 7 … … 30 32 private static Channel channel; 31 33 private static boolean clientStarted = false; 32 33 public static void initBroker(Properties env) throws Exception { 34 private static boolean connectionClosed = false; 35 // TODO ask Pedro if it can be only one object in the map (an object can 36 // have multiple threads in the same broker -see environment-) 37 private static Map<String, RemoteObject> remoteObjs; 38 39 /** 40 * Initializes a new Broker with the environment called by reference 41 * 42 * @param env 43 * @throws Exception 44 */ 45 public static synchronized void initBroker(Properties env) throws Exception { 34 46 if (Environment.isVoid()) { 47 remoteObjs = new HashMap<String, RemoteObject>(); 35 48 Environment.setEnvironment(env); 36 49 connection = OmqConnectionFactory.getNewConnection(env); … … 44 57 throw new InitBrokerException("The connection didn't work"); 45 58 } 46 } 47 } 48 49 // TODO: what happens if the connection is not set 59 } else { 60 throw new InitBrokerException("Broker already started"); 61 } 62 } 63 64 public static void stopBroker() throws Exception { 65 // Stop the client 66 if (clientStarted) { 67 ResponseListener.stopResponseListner(); 68 EventDispatcher.stopEventDispatcher(); 69 } 70 // Stop all the remote objects working 71 for (String reference : remoteObjs.keySet()) { 72 unbind(reference); 73 } 74 // Close the connection once all the listeners are died 75 closeConnection(); 76 } 77 78 /** 79 * @return Broker's connection 80 * @throws Exception 81 */ 50 82 public static Connection getConnection() throws Exception { 51 83 return connection; 52 84 } 53 85 86 public static void closeConnection() throws IOException { 87 connectionClosed = true; 88 connection.close(); 89 } 90 91 /** 92 * 93 * @return Broker's channel 94 * @throws Exception 95 */ 54 96 public static Channel getChannel() throws Exception { 55 97 return channel; 56 98 } 57 99 100 /** 101 * Creates a new channel using the Broker's connection 102 * 103 * @return newChannel 104 * @throws IOException 105 */ 58 106 public static Channel getNewChannel() throws IOException { 59 107 return connection.createChannel(); … … 85 133 Properties environment = Environment.getEnvironment(); 86 134 remote.startRemoteObject(reference, environment); 135 remoteObjs.put(reference, remote); 87 136 } catch (Exception e) { 88 137 throw new RemoteException(e); … … 90 139 } 91 140 92 public static void unbind(String reference) throws RemoteException { 141 public static void unbind(String reference) throws RemoteException, IOException { 142 if (remoteObjs.containsKey(reference)) { 143 RemoteObject remote = remoteObjs.get(reference); 144 remote.kill(); 145 } else { 146 throw new RemoteException("The object referenced by 'reference' does not exist in the Broker"); 147 } 93 148 94 149 } … … 98 153 } 99 154 155 /** 156 * This method ensures the client will have only one ResponseListener and 157 * only one EventDispatcher. Both with the same environment. 158 * 159 * @param environment 160 * @throws Exception 161 */ 100 162 private static synchronized void initClient(Properties environment) throws Exception { 101 163 if (ResponseListener.isVoid()) { … … 107 169 } 108 170 171 /** 172 * This method sends an event with its information 173 * 174 * @param event 175 * @throws IOException 176 * @throws SerializerException 177 */ 109 178 public static void trigger(Event event) throws IOException, SerializerException { 110 179 String UID = event.getTopic(); … … 119 188 } 120 189 190 /** 191 * This function is used to send a ping message to see if the connection 192 * works 193 * 194 * @param env 195 * @throws Exception 196 */ 121 197 public static void tryConnection(Properties env) throws Exception { 198 System.out.println("hola"); 122 199 Channel channel = connection.createChannel(); 123 200 String message = "ping"; … … 148 225 } 149 226 227 /** 228 * This method adds a ShutdownListener to the Broker's connection. When this 229 * connection falls, a new connection will be created and this will also 230 * have the listener. 231 */ 150 232 private static void addFaultTolerance() { 151 233 connection.addShutdownListener(new ShutdownListener() { 152 234 @Override 153 235 public void shutdownCompleted(ShutdownSignalException cause) { 154 155 if (cause.isHardError()) { 156 if (connection.isOpen()) { 236 if (!connectionClosed) 237 if (cause.isHardError()) { 238 if (connection.isOpen()) { 239 try { 240 connection.close(); 241 } catch (IOException e) { 242 e.printStackTrace(); 243 } 244 } 157 245 try { 158 connection.close(); 159 } catch (IOException e) { 246 Properties env = Environment.getEnvironment(); 247 connection = OmqConnectionFactory.getNewWorkingConnection(env); 248 channel = connection.createChannel(); 249 addFaultTolerance(); 250 } catch (Exception e) { 160 251 e.printStackTrace(); 161 252 } 162 } 163 try { 164 Properties env = Environment.getEnvironment(); 165 connection = OmqConnectionFactory.getNewWorkingConnection(env); 166 channel = connection.createChannel(); 167 addFaultTolerance(); 168 } catch (Exception e) { 169 e.printStackTrace(); 170 } 171 } else { 172 Channel channel = (Channel) cause.getReference(); 173 if (channel.isOpen()) { 174 try { 175 channel.close(); 176 } catch (IOException e) { 177 e.printStackTrace(); 253 } else { 254 Channel channel = (Channel) cause.getReference(); 255 if (channel.isOpen()) { 256 try { 257 channel.close(); 258 } catch (IOException e) { 259 e.printStackTrace(); 260 } 178 261 } 179 262 } 180 }181 263 } 182 264 }); -
trunk/objectmq/src/omq/server/InvocationThread.java
r35 r37 69 69 } 70 70 71 } 71 }System.out.println("Invocation Thread dies!!"); 72 72 } 73 73 -
trunk/objectmq/src/omq/server/RemoteObject.java
r35 r37 129 129 130 130 public void kill() throws IOException { 131 killed = true; 131 132 interrupt(); 132 killed = true;133 133 channel.close(); 134 134 remoteWrapper.stopRemoteWrapper(); -
trunk/objectmq/test/faultToleranceTest/ClientTest.java
r36 r37 1 package clientToleranceTest;1 package faultToleranceTest; 2 2 3 3 import static org.junit.Assert.assertEquals; -
trunk/objectmq/test/faultToleranceTest/ServerTest.java
r36 r37 1 package clientToleranceTest;1 package faultToleranceTest; 2 2 3 3 import java.util.Properties; -
trunk/objectmq/test/multiThreadTest/Car.java
r29 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.List; -
trunk/objectmq/test/multiThreadTest/CarImpl.java
r34 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.List; -
trunk/objectmq/test/multiThreadTest/CarThread.java
r29 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.ArrayList; -
trunk/objectmq/test/multiThreadTest/ClientTest.java
r29 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.Properties; -
trunk/objectmq/test/multiThreadTest/Mobile.java
r29 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.List; -
trunk/objectmq/test/multiThreadTest/MobileImpl.java
r34 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.ArrayList; -
trunk/objectmq/test/multiThreadTest/MobileThread.java
r29 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import omq.common.broker.Broker; -
trunk/objectmq/test/multiThreadTest/Rim.java
r28 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.io.Serializable; -
trunk/objectmq/test/multiThreadTest/ServerTest.java
r29 r37 1 package test2;1 package multiThreadTest; 2 2 3 3 import java.util.Properties; -
trunk/objectmq/test/test/Client.java
r27 r37 25 25 26 26 @AsyncMethod 27 public void sendContact(String contact) throws RemoteException;27 public void addContact(String contact) throws RemoteException; 28 28 } -
trunk/objectmq/test/test/ClientImpl.java
r34 r37 53 53 @Override 54 54 @AsyncMethod 55 public void sendContact(String contact) throws RemoteException {55 public void addContact(String contact) throws RemoteException { 56 56 if (!id.equalsIgnoreCase(contact) && !friendList.containsKey(contact)) { 57 57 Client client = (Client) Broker.lookup(contact, Client.class);
Note: See TracChangeset
for help on using the changeset viewer.