close #102 Logging, Queue
This commit is contained in:
parent
c8984803c7
commit
d4457e3d53
@ -6,9 +6,14 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class CommandShell {
|
public class CommandShell {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger("de.hsadmin.core.qserv");
|
||||||
|
|
||||||
|
|
||||||
public static String execute(String command) throws ShellException {
|
public static String execute(String command) throws ShellException {
|
||||||
return execute(command, null);
|
return execute(command, null);
|
||||||
}
|
}
|
||||||
@ -23,10 +28,7 @@ public class CommandShell {
|
|||||||
logCommand += "<<EOF\n" + stdInput + "EOF";
|
logCommand += "<<EOF\n" + stdInput + "EOF";
|
||||||
}
|
}
|
||||||
logCommand += "\n";
|
logCommand += "\n";
|
||||||
// TODO logging
|
logger.log(Level.INFO, logCommand);
|
||||||
System.out.println("--- shell command: ---");
|
|
||||||
System.out.println(logCommand);
|
|
||||||
|
|
||||||
String[] cmdArray = { "/bin/bash", "-c", command };
|
String[] cmdArray = { "/bin/bash", "-c", command };
|
||||||
backend = Runtime.getRuntime().exec(cmdArray);
|
backend = Runtime.getRuntime().exec(cmdArray);
|
||||||
if (stdInput != null) {
|
if (stdInput != null) {
|
||||||
@ -38,22 +40,12 @@ public class CommandShell {
|
|||||||
}
|
}
|
||||||
callOutput = readProcessStream(backend.getInputStream());
|
callOutput = readProcessStream(backend.getInputStream());
|
||||||
exitCode = backend.waitFor();
|
exitCode = backend.waitFor();
|
||||||
|
logger.log(Level.INFO, "Return-Code: " + exitCode);
|
||||||
// TODO logging
|
|
||||||
System.out.println("RET: " + exitCode);
|
|
||||||
|
|
||||||
if (exitCode != 0) {
|
if (exitCode != 0) {
|
||||||
String aErr = readProcessStream(backend.getErrorStream());
|
String aErr = readProcessStream(backend.getErrorStream());
|
||||||
|
logger.log(Level.SEVERE, aErr);
|
||||||
// TODO logging
|
|
||||||
System.out.println("ERR: " + aErr);
|
|
||||||
System.out.println("--- done. ---");
|
|
||||||
|
|
||||||
throw new ShellException(aErr);
|
throw new ShellException(aErr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO logging
|
|
||||||
System.out.println("--- done. ---");
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ShellException(e);
|
throw new ShellException(e);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
@ -2,9 +2,9 @@ package de.hsadmin.core.qserv;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.jms.Connection;
|
import javax.jms.Connection;
|
||||||
import javax.jms.ConnectionFactory;
|
import javax.jms.ConnectionFactory;
|
||||||
@ -28,6 +28,7 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
|
|||||||
|
|
||||||
private static final String VERSION_NO = "1.1";
|
private static final String VERSION_NO = "1.1";
|
||||||
|
|
||||||
|
private Logger logger;
|
||||||
private QueueConnection conn;
|
private QueueConnection conn;
|
||||||
private QueueSession queueSession;
|
private QueueSession queueSession;
|
||||||
private String jmsStatusQueue;
|
private String jmsStatusQueue;
|
||||||
@ -35,57 +36,39 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
|
|||||||
private String jmsUserName;
|
private String jmsUserName;
|
||||||
private String jmsSystemQueue;
|
private String jmsSystemQueue;
|
||||||
private String jmsFactory;
|
private String jmsFactory;
|
||||||
|
|
||||||
private String serviceEMail;
|
private String serviceEMail;
|
||||||
|
|
||||||
/** Runs the QueueServer, using the arguments as ConnectionFactory
|
/**
|
||||||
|
* Runs the QueueServer, using the arguments as ConnectionFactory
|
||||||
* and Topic names.
|
* and Topic names.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
File propFile = new File(System.getProperty("user.dir"), "conf/qserv.properties");
|
File propFile = new File(System.getProperty("user.dir"), "conf/qserv.properties");
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
propFile = new File(args[0]);
|
propFile = new File(args[0]);
|
||||||
|
} else {
|
||||||
|
if (args.length != 0) {
|
||||||
|
throw new Exception(userHelp(propFile));
|
||||||
}
|
}
|
||||||
else if (args.length != 0) {
|
|
||||||
throw new Exception(
|
|
||||||
"Wrong number of arguments.\n"
|
|
||||||
+ "With no arguments '"
|
|
||||||
+ propFile
|
|
||||||
+ "' will be used as config file.\n"
|
|
||||||
+ "Or give a properties file as single argument.\n\n"
|
|
||||||
+ "Example config file:\n\n"
|
|
||||||
+ "hsadmin.jms.factory=QueueCF\n"
|
|
||||||
+ "hsadmin.jms.system-queue=hive-h01\n"
|
|
||||||
+ "hsadmin.jms.status-queue=queue/hsadminStatus\n"
|
|
||||||
+ "hsadmin.jms.username=hive-h01\n"
|
|
||||||
+ "hsadmin.jms.password=geheimeskennwort\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInputStream propStream = new FileInputStream(propFile);
|
FileInputStream propStream = new FileInputStream(propFile);
|
||||||
Properties props = new Properties(System.getProperties());
|
Properties props = new Properties(System.getProperties());
|
||||||
props.load(propStream);
|
props.load(propStream);
|
||||||
propStream.close();
|
propStream.close();
|
||||||
String stdout = System.getProperty("hsadmin.stdout");
|
System.setProperty("java.util.logging.config.file", props.getProperty("hsadmin.log.file", System.getProperty("user.home") + "/.hsadmin.log.properties"));
|
||||||
if (stdout != null && stdout.length() > 0)
|
|
||||||
System.setOut(new PrintStream(new FileOutputStream(stdout)));
|
|
||||||
String stderr = System.getProperty("hsadmin.stderr");
|
|
||||||
if (stderr != null && stderr.length() > 0)
|
|
||||||
System.setErr(new PrintStream(new FileOutputStream(stderr)));
|
|
||||||
|
|
||||||
final QueueServer qServ = new QueueServer();
|
final QueueServer qServ = new QueueServer();
|
||||||
qServ.setJmsFactory(props.getProperty("hsadmin.jms.factory"));
|
qServ.setJmsFactory(props.getProperty("hsadmin.jms.factory"));
|
||||||
qServ.setJmsSystemQueue(props.getProperty("hsadmin.jms.system-queue"));
|
qServ.setJmsSystemQueue(props.getProperty("hsadmin.jms.system-queue"));
|
||||||
qServ.setJmsStatusQueue(props.getProperty("hsadmin.jms.status-queue"));
|
qServ.setJmsStatusQueue(props.getProperty("hsadmin.jms.status-queue"));
|
||||||
qServ.setJmsUserName(props.getProperty("hsadmin.jms.username"));
|
qServ.setJmsUserName(props.getProperty("hsadmin.jms.username"));
|
||||||
qServ.setJmsPassWord(props.getProperty("hsadmin.jms.password"));
|
qServ.setJmsPassWord(props.getProperty("hsadmin.jms.password"));
|
||||||
qServ.setServiceEMail(props.getProperty("hsadmin.email.service"));
|
qServ.setServiceEMail(props.getProperty("hsadmin.log.email"));
|
||||||
System.out.println("==================================================");
|
Logger logger = Logger.getLogger("de.hsadmin.core.qserv");
|
||||||
System.out.println("hsadmin-qserv " + VERSION_NO + " started using:");
|
logger.log(Level.CONFIG, "hsadmin-qserv " + VERSION_NO + " started using:"
|
||||||
System.out.println("queue server: " + props.getProperty("hsadmin.jms.factory"));
|
+ "\nqueue server: " + props.getProperty("hsadmin.jms.factory")
|
||||||
System.out.println("system queue: " + props.getProperty("hsadmin.jms.system-queue"));
|
+ "\nsystem queue: " + props.getProperty("hsadmin.jms.system-queue")
|
||||||
System.out.println("status queue: " + props.getProperty("hsadmin.jms.status-queue"));
|
+ "\nstatus queue: " + props.getProperty("hsadmin.jms.status-queue")
|
||||||
System.out.println("queue user: " + props.getProperty("hsadmin.jms.username"));
|
+ "\nqueue user: " + props.getProperty("hsadmin.jms.username"));
|
||||||
System.out.println("==================================================");
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -100,11 +83,29 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setServiceEMail(String property) {
|
private static String userHelp(File propFile) {
|
||||||
if (property == null) {
|
return "Wrong number of arguments.\n"
|
||||||
|
+ "With no arguments '"
|
||||||
|
+ propFile
|
||||||
|
+ "' will be used as config file.\n"
|
||||||
|
+ "Or give a properties file as single argument.\n\n"
|
||||||
|
+ "Example config file:\n\n"
|
||||||
|
+ "hsadmin.jms.factory=QueueCF\n"
|
||||||
|
+ "hsadmin.jms.system-queue=hive-h01\n"
|
||||||
|
+ "hsadmin.jms.status-queue=queue/hsadminStatus\n"
|
||||||
|
+ "hsadmin.jms.username=hive-h01\n"
|
||||||
|
+ "hsadmin.jms.password=geheimeskennwort\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueueServer() {
|
||||||
|
logger = Logger.getLogger("de.hsadmin.core.qserv");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setServiceEMail(String emailAddress) {
|
||||||
|
if (emailAddress == null || emailAddress.isEmpty()) {
|
||||||
serviceEMail = "peter@ostwall195.de";
|
serviceEMail = "peter@ostwall195.de";
|
||||||
} else {
|
} else {
|
||||||
serviceEMail = property;
|
serviceEMail = emailAddress;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,26 +124,24 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
|
|||||||
receiver.setMessageListener(this);
|
receiver.setMessageListener(this);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.getMessage());
|
logger.log(Level.WARNING, e.getMessage(), e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void onMessage(Message jmsMessage) {
|
public synchronized void onMessage(Message jmsMessage) {
|
||||||
// TODO logging
|
logger.log(Level.INFO, jmsMessage.toString());
|
||||||
System.out.println("\nonMessage(" + jmsMessage);
|
|
||||||
QueueTask task = null;
|
QueueTask task = null;
|
||||||
try {
|
try {
|
||||||
ObjectMessage jmsObjectMessage = (ObjectMessage) jmsMessage;
|
ObjectMessage jmsObjectMessage = (ObjectMessage) jmsMessage;
|
||||||
task = (QueueTask) jmsObjectMessage.getObject();
|
task = (QueueTask) jmsObjectMessage.getObject();
|
||||||
Processor processor = task.getProcessor();
|
Processor processor = task.getProcessor();
|
||||||
System.out.println("processing (" + task.getTitle() + " | started("
|
logger.log(Level.INFO, "processing (" + task.getTitle() + " | started("
|
||||||
+ task.getStarted() + ") |" + task.getDetails() + "|"
|
+ task.getStarted() + ") |" + task.getDetails() + "|"
|
||||||
+ processor + ")");
|
+ processor + ")");
|
||||||
System.out.println("with " + processor);
|
|
||||||
try {
|
try {
|
||||||
processor.process();
|
processor.process();
|
||||||
System.out.println("done");
|
logger.log(Level.INFO, "done");
|
||||||
} catch (ProcessorException e) {
|
} catch (ProcessorException e) {
|
||||||
logException(e);
|
logException(e);
|
||||||
task.setException(e);
|
task.setException(e);
|
||||||
@ -158,19 +157,16 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void logException(Throwable t) {
|
private void logException(Throwable t) {
|
||||||
System.err.println("exception " + t); // TODO: logging
|
logger.log(Level.SEVERE, t.getMessage(), t);
|
||||||
t.printStackTrace(System.err);
|
Throwable cause = t.getCause();
|
||||||
if (t.getCause() != null) {
|
if (cause != null) {
|
||||||
System.err.println("caused by exception "
|
logger.log(Level.SEVERE, "cause: " + t.getMessage(), t);
|
||||||
+ t.getCause()); // TODO: logging
|
}
|
||||||
t.getCause().printStackTrace(System.err);
|
|
||||||
} else
|
|
||||||
System.err.println("no further cause available");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onException(JMSException e) {
|
public void onException(JMSException e) {
|
||||||
System.out.println("Exception: " + e.getMessage());
|
logger.log(Level.WARNING, e.getMessage(), e);
|
||||||
close();
|
close();
|
||||||
while (!connect()) {
|
while (!connect()) {
|
||||||
try {
|
try {
|
||||||
@ -197,22 +193,18 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
|
|||||||
Session statusSession = null;
|
Session statusSession = null;
|
||||||
Connection statusConnection = null;
|
Connection statusConnection = null;
|
||||||
try {
|
try {
|
||||||
System.out.println("sendStatus( " + queueMessage + ")");
|
logger.log(Level.INFO, "sendStatus(" + queueMessage + ")");
|
||||||
Context ctx = new InitialContext();
|
Context ctx = new InitialContext();
|
||||||
System.out.println("Created InitialContext");
|
|
||||||
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(jmsFactory);
|
ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(jmsFactory);
|
||||||
System.out.println("connectionFactory= " + connectionFactory.toString());
|
|
||||||
Destination queue = (Destination) ctx.lookup(jmsStatusQueue);
|
Destination queue = (Destination) ctx.lookup(jmsStatusQueue);
|
||||||
statusConnection = connectionFactory.createConnection(jmsUserName, jmsPassWord);
|
statusConnection = connectionFactory.createConnection(jmsUserName, jmsPassWord);
|
||||||
statusSession = statusConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
statusSession = statusConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||||
producer = statusSession.createProducer(queue);
|
producer = statusSession.createProducer(queue);
|
||||||
ObjectMessage statusMessage = statusSession.createObjectMessage(queueMessage);
|
ObjectMessage statusMessage = statusSession.createObjectMessage(queueMessage);
|
||||||
System.out.println("send( " + statusMessage + ")");
|
logger.log(Level.INFO, "send(" + statusMessage + ")");
|
||||||
producer.send(statusMessage);
|
producer.send(statusMessage);
|
||||||
} catch (Exception statusException) {
|
} catch (Exception statusException) {
|
||||||
// TODO: log exception
|
logger.log(Level.SEVERE, statusException.getMessage(), statusException);
|
||||||
System.out.println("failed");
|
|
||||||
statusException.printStackTrace(System.err);
|
|
||||||
} finally {
|
} finally {
|
||||||
// close JMS
|
// close JMS
|
||||||
try { producer.close(); } catch (Exception exc) { }
|
try { producer.close(); } catch (Exception exc) { }
|
||||||
|
@ -43,7 +43,8 @@ public class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getProperty(String propertyName) {
|
public String getProperty(String propertyName) {
|
||||||
return props.getProperty(propertyName).trim();
|
String property = props.getProperty(propertyName);
|
||||||
|
return property;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProperty(String propertyName, String defaultValue) {
|
public String getProperty(String propertyName, String defaultValue) {
|
||||||
|
Loading…
Reference in New Issue
Block a user