1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.miloss.fgsms.agents.qpidpy;
22
23 import java.io.File;
24 import java.io.RandomAccessFile;
25 import java.nio.channels.FileChannel;
26 import java.nio.channels.FileLock;
27 import org.apache.log4j.Level;
28 import org.miloss.fgsms.common.Logger;;
29 import org.apache.log4j.PropertyConfigurator;
30 import org.miloss.fgsms.agentcore.ConfigurationException;
31 import org.miloss.fgsms.agentcore.StatusHelper;
32 import org.miloss.fgsms.common.IpAddressUtility;
33 import org.miloss.fgsms.common.Utility;
34 import org.miloss.fgsms.services.interfaces.common.PolicyType;
35
36
37
38
39
40
41
42
43 public class Mainv2 {
44
45 static Logger log = Logger.getLogger("fgsms.QpidPython");
46 private boolean running = true;
47 private boolean done = false;
48 private File file;
49 private FileChannel channel;
50 private FileLock lock;
51
52 private void closeLock() {
53 try {
54 lock.release();
55 } catch (Exception e) {
56 }
57 try {
58 channel.close();
59 } catch (Exception e) {
60 }
61 }
62
63 private void deleteFile() {
64 try {
65 file.delete();
66 } catch (Exception e) {
67 }
68 }
69
70 private void Run(String[] args) throws ConfigurationException {
71
72 try {
73 file = new File("amqp.lck");
74 channel = new RandomAccessFile(file, "rw").getChannel();
75 lock = channel.tryLock();
76 } catch (Exception e) {
77
78 closeLock();
79 System.out.println("Could not obtain the lock, this means that either this program is already running or something went wrong and the file amqp.lck needs to be deleted.");
80 return;
81 }
82 if (lock == null) {
83 closeLock();
84 System.out.println("Could not obtain the lock, this means that either this program is already running or something went wrong and the file amqp.lck needs to be deleted.");
85 return;
86 }
87
88 Runtime.getRuntime().addShutdownHook(new RunWhenShuttingDown());
89 PropertyConfigurator.configure("log4j.properties");
90 long interval = 10000;
91 if (args.length == 1) {
92 try {
93 interval = Long.parseLong(args[0]);
94 if (interval < 10000) {
95 interval = 10000;
96 }
97 } catch (Exception ex) {
98 }
99 }
100
101
102 String url = IpAddressUtility.modifyURL("amqp://localhost", false);
103 qpidcmdws q = new qpidcmdws();
104 while (running) {
105 try {
106
107
108
109
110
111 q.Fire(url);
112
113
114
115
116
117
118
119
120 } catch (Exception ex) {
121 log.log(Level.ERROR, ex);
122
123 }
124 if (running) {
125 try {
126 log.log(Level.INFO, "Sleeping " + interval + "ms until next interation....");
127 Thread.sleep(interval);
128 } catch (InterruptedException ex) {
129 log.log(Level.ERROR, null, ex);
130 }
131 }
132
133 }
134 done = true;
135
136 }
137
138
139 public class RunWhenShuttingDown extends Thread {
140
141 public void run() {
142 System.out.println("Control-C caught. Shutting down...");
143
144 running = false;
145 while (!done) {
146 try {
147 Thread.sleep(1000);
148 } catch (InterruptedException ex) {
149 }
150 }
151 closeLock();
152 deleteFile();
153 try {
154 boolean TryUpdateStatus = StatusHelper.tryUpdateStatus(false, IpAddressUtility.modifyURL("amqp://localhost", false), "Agent Stopped", false, PolicyType.STATISTICAL, null, Utility.getHostName());
155 if (!TryUpdateStatus) {
156 System.out.println("unable to send the last statue update, see log output for details ");
157 }
158
159 } catch (ConfigurationException ex) {
160 System.out.println("unable to send the last statue update " + ex.getMessage());
161 }
162 }
163 }
164
165
166
167
168 public static void main(String[] args) throws ConfigurationException {
169 new Mainv2().Run(args);
170
171 }
172 }