|
@@ -0,0 +1,78 @@
|
|
|
+package com.redhat.training.sender;
|
|
|
+
|
|
|
+import java.lang.management.ManagementFactory;
|
|
|
+import java.lang.management.ThreadMXBean;
|
|
|
+
|
|
|
+import javax.jms.Connection;
|
|
|
+import javax.jms.ConnectionFactory;
|
|
|
+import javax.jms.JMSException;
|
|
|
+import javax.jms.MessageProducer;
|
|
|
+import javax.jms.Queue;
|
|
|
+import javax.jms.Session;
|
|
|
+import javax.jms.TextMessage;
|
|
|
+import javax.naming.InitialContext;
|
|
|
+import javax.naming.NamingException;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+public class SendMetric {
|
|
|
+ public static void main(String... args) {
|
|
|
+ Logger LOG = LoggerFactory.getLogger(SendMetric.class);
|
|
|
+
|
|
|
+ // prepare metrics objects
|
|
|
+ ThreadMXBean thr = ManagementFactory.getThreadMXBean();
|
|
|
+ long processId = ManagementFactory.getRuntimeMXBean().getPid();
|
|
|
+ long threadId = Thread.currentThread().getId();
|
|
|
+ long oldTime = 0;
|
|
|
+
|
|
|
+ // prepare connection objects
|
|
|
+ InitialContext ctx;
|
|
|
+ ConnectionFactory cf;
|
|
|
+ Connection c;
|
|
|
+ Session s;
|
|
|
+ MessageProducer mp;
|
|
|
+ Queue q;
|
|
|
+
|
|
|
+ try {
|
|
|
+ ctx = new InitialContext();
|
|
|
+ cf = (ConnectionFactory)ctx.lookup("ConnectionFactory");
|
|
|
+ q = (Queue)ctx.lookup("queue/metrics");
|
|
|
+ } catch (NamingException ne) {
|
|
|
+ ne.printStackTrace();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ c = cf.createConnection();
|
|
|
+ c.start();
|
|
|
+
|
|
|
+ s = c.createSession();
|
|
|
+ mp = s.createProducer(q);
|
|
|
+ } catch (JMSException je) {
|
|
|
+ je.printStackTrace();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ long newTime = thr.getThreadCpuTime(threadId);
|
|
|
+ String report = processId + "[" + threadId + "]: Current CPU time: " + (newTime - oldTime);
|
|
|
+
|
|
|
+ LOG.info(report);
|
|
|
+
|
|
|
+ try {
|
|
|
+ TextMessage tm = s.createTextMessage(report);
|
|
|
+ mp.send(tm);
|
|
|
+ } catch (JMSException je) {
|
|
|
+ je.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ oldTime = newTime;
|
|
|
+ try {
|
|
|
+ Thread.sleep(1000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|