[33] | 1 | package omq.common.remote; |
---|
| 2 | |
---|
| 3 | import java.io.FileInputStream; |
---|
| 4 | import java.io.IOException; |
---|
| 5 | import java.util.Properties; |
---|
| 6 | |
---|
| 7 | import omq.common.util.OmqConnectionFactory; |
---|
| 8 | |
---|
| 9 | import com.rabbitmq.client.Channel; |
---|
| 10 | import com.rabbitmq.client.Connection; |
---|
| 11 | import com.rabbitmq.client.QueueingConsumer; |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * |
---|
| 15 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | //TODO aquesta classe es pot eliminar |
---|
| 19 | public abstract class RemoteListener extends Thread { |
---|
| 20 | private static String defaultXml = "eventListener.xml"; |
---|
| 21 | |
---|
| 22 | protected Connection connection; |
---|
| 23 | protected Channel channel; |
---|
| 24 | protected QueueingConsumer consumer; |
---|
| 25 | protected boolean killed = false; |
---|
| 26 | |
---|
| 27 | protected RemoteListener() throws Exception { |
---|
| 28 | // Load the default environment |
---|
| 29 | FileInputStream fis = new FileInputStream(defaultXml); |
---|
| 30 | Properties env = new Properties(); |
---|
| 31 | env.loadFromXML(fis); |
---|
| 32 | fis.close(); |
---|
| 33 | |
---|
| 34 | startConnection(env); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | protected RemoteListener(Properties env) throws Exception { |
---|
| 38 | startConnection(env); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | private void startConnection(Properties env) throws Exception { |
---|
| 42 | connection = OmqConnectionFactory.getNewConnection(env); |
---|
| 43 | channel = connection.createChannel(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public synchronized Channel getChannel() throws Exception { |
---|
| 47 | return connection.createChannel(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Interrupt and kill the Thread |
---|
| 52 | * |
---|
| 53 | * @throws IOException |
---|
| 54 | */ |
---|
| 55 | public void kill() throws IOException { |
---|
| 56 | interrupt(); |
---|
| 57 | killed = true; |
---|
| 58 | channel.close(); |
---|
| 59 | connection.close(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * TODO RESTART CONNECTION FAULT TOLERANCE |
---|
| 64 | */ |
---|
| 65 | |
---|
| 66 | } |
---|