Skip to content

Commit

Permalink
Merge pull request #316 from sumeetphadnis/next
Browse files Browse the repository at this point in the history
Enable status module
  • Loading branch information
ar authored Oct 1, 2024
2 parents fec9ddf + 4832869 commit 478ef3a
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.jpos.ee.status;

import java.net.Socket;
import java.io.IOException;
import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
Expand All @@ -31,17 +29,16 @@ public class ChannelMonitor extends Log implements TwoWayMonitorTask, Configurab
Configuration cfg;
String serviceName;
public String checkService () {
boolean rc = false;
ChannelAdaptor channel;
try {
channel = (ChannelAdaptor) NameRegistrar.get (serviceName);
channel = NameRegistrar.get (serviceName);
} catch (NameRegistrar.NotFoundException e) {
return Status.ERROR + " channel '" + serviceName + "' not found.";
return StatusBase.ERROR + " channel '" + serviceName + "' not found.";
}
if (channel.isConnected())
return Status.OK + " Connected";
return StatusBase.OK + " Connected";
else
return Status.ERROR + " Not connected";
return StatusBase.ERROR + " Not connected";
}
public String command (String command) {
String response = "Unknown command";
Expand Down
15 changes: 9 additions & 6 deletions modules/status/src/main/java/org/jpos/ee/status/Heartbeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.jpos.ee.status;

import java.util.Date;
import java.sql.SQLException;

import org.hibernate.Transaction;
import org.hibernate.HibernateException;

Expand All @@ -34,12 +34,15 @@ public class Heartbeat extends QBeanSupport implements Runnable {
long interval;
String statusId;

@Override
public void initService () throws Exception {
db = new DB();
mgr = new StatusManager (db);
interval = cfg.getLong ("interval", 60000L);
initStatus();
}

@Override
public void startService() {
new Thread (this).start();
}
Expand All @@ -50,9 +53,9 @@ public void run() {
db.open ();
if (cfg.getBoolean ("check", true))
mgr.check ();
mgr.touch (statusId, Status.OK, getDetail (start, i));
} catch (Throwable t) {
getLog().error (t);
mgr.touch (statusId, StatusBase.OK, getDetail (start, i));
} catch (Exception e) {
getLog().error (e);
ISOUtil.sleep (1000);
} finally {
close();
Expand Down Expand Up @@ -81,7 +84,7 @@ protected String getDetail (long start, int tick) {
return sb.toString();
}
private void initStatus()
throws HibernateException, SQLException
throws HibernateException
{
try {
db.open ();
Expand All @@ -92,7 +95,7 @@ private void initStatus()
if (s== null) s = mgr.getStatus (statusId, true);
s.setName (cfg.get ("status-name", statusId));
s.setGroupName (cfg.get ("status-group", ""));
s.setTimeoutState (cfg.get ("on-timeout", Status.OFF));
s.setTimeoutState (cfg.get ("on-timeout", StatusBase.OFF));
s.setTimeout (cfg.getLong ("status-timeout", 360000L));
s.setLastTick (new Date());
tx.commit();
Expand Down
123 changes: 64 additions & 59 deletions modules/status/src/main/java/org/jpos/ee/status/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

package org.jpos.ee.status;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Iterator;
import java.util.ArrayList;

import org.jpos.ee.DB;
import org.jpos.q2.QFactory;
import org.jpos.q2.QBeanSupport;
Expand All @@ -40,32 +38,27 @@ public class Monitor extends QBeanSupport implements XmlConfigurable {
Element config;
Timer timer;

@Override
public void initService () {
db = new DB();
mgr = new StatusManager (db);
timer = new Timer (true);
Iterator iter = config.getChildren ("monitor").iterator();
List handlers = new ArrayList();
while (iter.hasNext()) {
Element e = (Element) iter.next();
config.getChildren ("monitor").forEach(e -> {
try {
registerTask (e);
} catch (ConfigurationException ex) {
getLog().error (ex);
}
}
});
}

@Override
public void destroyService() {
getLog().info ("Cancelling timer");
timer.cancel();
}
private void close() {
try {
db.close();
} catch (HibernateException e) {
getLog().error (e);
}
}

@Override
public void setConfiguration (Element config) {
this.config = config;
}
Expand Down Expand Up @@ -98,59 +91,71 @@ private long getLong (String l)
throw new ConfigurationException (e);
}
}
private synchronized void touch (String id, String detail) {
int sp = detail.indexOf (" ");
String state = Status.OFF;
if (sp > 0 && detail.length() > sp) {
state = detail.substring (0, sp);
detail = detail.substring (++sp);
}
try {
db.open ();
mgr.touch (id, state, detail);
} catch (Throwable t) {
getLog().error (t);
} finally {
close();
}
}
private synchronized String getNextCommand (String id) {
try {
db.open ();
return mgr.getNextCommand (id);
} catch (Throwable t) {
getLog().error (t);
} finally {
close();
}
return null;
}
private synchronized void setResponse (String id, String response) {
try {
db.open ();
mgr.setResponse (id, response);
} catch (Throwable t) {
getLog().error (t);
} finally {
close();
}
}
public class MonitorTimerTask extends TimerTask {
String id;
MonitorTask task;
public MonitorTimerTask (String id, MonitorTask task) {

public MonitorTimerTask(String id, MonitorTask task) {
super();
this.id = id;
this.task = task;
}

public void run() {
if (task instanceof TwoWayMonitorTask) {
String command = getNextCommand (id);
if (command != null)
setResponse (id,
((TwoWayMonitorTask)task).command (command));
if (task instanceof TwoWayMonitorTask t) {
String command = getNextCommand(id);
if (command != null)
setResponse(id, t.command(command));
}
touch(id, task.checkService());
}

private synchronized void touch(String id, String detail) {
int sp = detail.indexOf(" ");
String state = StatusBase.OFF;
if (sp > 0 && detail.length() > sp) {
state = detail.substring(0, sp);
detail = detail.substring(++sp);
}
try {
db.open();
mgr.touch(id, state, detail);
} catch (Exception e) {
getLog().error(e);
} finally {
close();
}
}

private synchronized String getNextCommand(String id) {
try {
db.open();
return mgr.getNextCommand(id);
} catch (Exception e) {
getLog().error(e);
} finally {
close();
}
return null;
}

private synchronized void setResponse(String id, String response) {
try {
db.open();
mgr.setResponse(id, response);
} catch (Exception e) {
getLog().error(e);
} finally {
close();
}
}

private void close() {
try {
db.close();
} catch (HibernateException e) {
getLog().error(e);
}
touch (id, task.checkService());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
package org.jpos.ee.status;

public interface MonitorTask {
public String checkService ();
String checkService();
}

2 changes: 1 addition & 1 deletion modules/status/src/main/java/org/jpos/ee/status/Ping.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String checkService () {
detail = " " + msg;
}
long elapsed = System.currentTimeMillis() - start;
return (rc ? Status.OK : Status.WARN) + detail
return (rc ? StatusBase.OK : StatusBase.WARN) + detail
+ " time=" + elapsed + "ms";
}
public void setConfiguration (Configuration cfg)
Expand Down
36 changes: 18 additions & 18 deletions modules/status/src/main/java/org/jpos/ee/status/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import java.util.Iterator;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jpos.syslog.SysLog;


/** @author Hibernate CodeGenerator */
Expand Down Expand Up @@ -65,19 +66,19 @@ public class Status extends org.jpos.ee.status.StatusBase implements Serializabl
private int maxEvents;

/** persistent field */
private Set events;
private Set<SysLog> events;

/** persistent field */
private Set revisions;
private Set<StatusRevision> revisions;

private Set tags;
private Set<StatusTag> tags;

/** default constructor */
public Status() {
}

/** minimal constructor */
public Status(String id, Set events, Set revisions) {
public Status(String id, Set<SysLog> events, Set<StatusRevision> revisions) {
this.id = id;
this.events = events;
this.revisions = revisions;
Expand Down Expand Up @@ -179,36 +180,35 @@ public void setMaxEvents(int maxEvents) {
this.maxEvents = maxEvents;
}

public Set getEvents() {
public Set<SysLog> getEvents() {
return this.events;
}

public void setEvents(Set events) {
public void setEvents(Set<SysLog> events) {
this.events = events;
}

public Set getRevisions() {
public Set<StatusRevision> getRevisions() {
return this.revisions;
}

public void setTags (Set tags) {
public void setTags (Set<StatusTag> tags) {
this.tags = tags;
}
public Set getTags () {
public Set<StatusTag> getTags () {
return this.tags;
}
public String getTagsAsString () {
StringBuffer sb = new StringBuffer();
Iterator it = getTags().iterator();
while (it.hasNext()) {
if (sb.length()>0) {
StringBuilder sb = new StringBuilder();
getTags().forEach(tag -> {
if (!sb.isEmpty()) {
sb.append(" ");
}
sb.append(((StatusTag)it.next()).getTag());
}
}
sb.append(tag.getTag());
});
return sb.toString();
}
public void setRevisions(Set revisions) {
public void setRevisions(Set<StatusRevision> revisions) {
this.revisions = revisions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class StatusBase {
public static final String CRITICAL = "CRITICAL";
public static final String[] possibleStates = { OFF, OK, WARN, ERROR, CRITICAL };

public static final Map icons = new HashMap();
public static final Map<String, String> icons = new HashMap<>();

static {
icons.put (OK, "green.gif");
Expand All @@ -51,14 +51,14 @@ public abstract class StatusBase {
public abstract String getState();
public abstract Date getLastTick();
public abstract long getTimeout();
public abstract Set getRevisions();
public abstract Set<StatusRevision> getRevisions();
public abstract String getValidCommands();

public String getIconName() {
String state = getState();
String icon = null;
if (state != null)
icon = (String) icons.get (state);
icon = icons.get (state);
return icon != null ? icon : "red.gif";
}
/**
Expand Down
Loading

0 comments on commit 478ef3a

Please sign in to comment.