[9] | 1 | package omq.common.broker; |
---|
| 2 | |
---|
[14] | 3 | import java.io.IOException; |
---|
[9] | 4 | import java.util.Properties; |
---|
| 5 | |
---|
| 6 | import omq.Remote; |
---|
| 7 | import omq.client.proxy.Proxymq; |
---|
| 8 | import omq.client.remote.response.ResponseListener; |
---|
[18] | 9 | import omq.common.event.Event; |
---|
[15] | 10 | import omq.common.event.EventDispatcher; |
---|
[18] | 11 | import omq.common.event.EventWrapper; |
---|
[11] | 12 | import omq.common.util.Environment; |
---|
[14] | 13 | import omq.common.util.OmqConnectionFactory; |
---|
[18] | 14 | import omq.common.util.Serializer; |
---|
[11] | 15 | import omq.exception.EnvironmentException; |
---|
[9] | 16 | import omq.exception.RemoteException; |
---|
[18] | 17 | import omq.exception.SerializerException; |
---|
[9] | 18 | import omq.server.remote.request.RemoteObject; |
---|
| 19 | |
---|
| 20 | import com.rabbitmq.client.Channel; |
---|
| 21 | import com.rabbitmq.client.Connection; |
---|
| 22 | |
---|
| 23 | public class Broker { |
---|
| 24 | private static Connection connection; |
---|
| 25 | private static Channel channel; |
---|
[15] | 26 | private static boolean clientStarted = false; |
---|
[9] | 27 | |
---|
| 28 | public static void initBroker(Properties env) throws Exception { |
---|
[11] | 29 | try { |
---|
| 30 | Environment.getEnvironment(); |
---|
| 31 | } catch (EnvironmentException ex) { // environment not set. |
---|
| 32 | Environment.setEnvironment(env); |
---|
[14] | 33 | connection = OmqConnectionFactory.getNewConnection(env); |
---|
[9] | 34 | channel = connection.createChannel(); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // TODO: what happens if the connection is not set |
---|
| 39 | public static Connection getConnection() throws Exception { |
---|
| 40 | return connection; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | public static Channel getChannel() throws Exception { |
---|
| 44 | return channel; |
---|
| 45 | } |
---|
| 46 | |
---|
[14] | 47 | public static Channel getNewChannel() throws IOException { |
---|
| 48 | return connection.createChannel(); |
---|
| 49 | } |
---|
| 50 | |
---|
[9] | 51 | public static Remote lookup(String reference, Class<?> contract) throws RemoteException { |
---|
| 52 | try { |
---|
[11] | 53 | Properties environment = Environment.getEnvironment(); |
---|
| 54 | |
---|
[15] | 55 | if (!clientStarted) { |
---|
| 56 | initClient(environment); |
---|
| 57 | clientStarted = true; |
---|
[9] | 58 | } |
---|
[15] | 59 | |
---|
[9] | 60 | if (!Proxymq.containsProxy(reference)) { |
---|
| 61 | Proxymq proxy = new Proxymq(reference, contract, environment); |
---|
| 62 | Class<?>[] array = { contract }; |
---|
| 63 | return (Remote) Proxymq.newProxyInstance(contract.getClassLoader(), array, proxy); |
---|
| 64 | } |
---|
| 65 | return (Remote) Proxymq.getInstance(reference); |
---|
| 66 | |
---|
| 67 | } catch (Exception e) { |
---|
| 68 | throw new RemoteException(e); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public static void bind(String reference, RemoteObject remote) throws RemoteException { |
---|
| 73 | try { |
---|
[11] | 74 | Properties environment = Environment.getEnvironment(); |
---|
[9] | 75 | remote.start(reference, environment); |
---|
| 76 | } catch (Exception e) { |
---|
| 77 | throw new RemoteException(e); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | public static void unbind(String reference) throws RemoteException { |
---|
| 82 | |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public void rebind(String name, Remote obj) throws RemoteException { |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | |
---|
[15] | 89 | private static void initClient(Properties environment) throws Exception { |
---|
| 90 | if (ResponseListener.isVoid()) { |
---|
| 91 | ResponseListener.init(environment); |
---|
| 92 | } |
---|
| 93 | if (EventDispatcher.isVoid()) { |
---|
| 94 | EventDispatcher.init(environment); |
---|
| 95 | } |
---|
| 96 | } |
---|
[18] | 97 | |
---|
| 98 | |
---|
| 99 | public static void trigger(Event event) throws IOException, SerializerException{ |
---|
| 100 | String UID = event.getTopic(); |
---|
| 101 | EventWrapper wrapper = new EventWrapper(event); |
---|
| 102 | channel.exchangeDeclare(UID, "fanout"); |
---|
| 103 | channel.basicPublish(UID, "", null, Serializer.serialize(wrapper)); |
---|
| 104 | } |
---|
[15] | 105 | |
---|
[9] | 106 | } |
---|