| | |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.PrintStream; |
| | | import java.util.Properties; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | |
| | | import javax.jms.Connection; |
| | | import javax.jms.ConnectionFactory; |
| | |
| | | |
| | | private static final String VERSION_NO = "1.1"; |
| | | |
| | | private Logger logger; |
| | | private QueueConnection conn; |
| | | private QueueSession queueSession; |
| | | private String jmsStatusQueue; |
| | |
| | | private String jmsUserName; |
| | | private String jmsSystemQueue; |
| | | private String jmsFactory; |
| | | |
| | | private String serviceEMail; |
| | | |
| | | /** Runs the QueueServer, using the arguments as ConnectionFactory |
| | | * and Topic names. |
| | | /** |
| | | * Runs the QueueServer, using the arguments as ConnectionFactory |
| | | * and Topic names. |
| | | */ |
| | | public static void main(String[] args) throws Exception { |
| | | File propFile = new File(System.getProperty("user.dir"), "conf/qserv.properties"); |
| | | if (args.length == 1) { |
| | | 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); |
| | | Properties props = new Properties(System.getProperties()); |
| | | props.load(propStream); |
| | | propStream.close(); |
| | | String stdout = System.getProperty("hsadmin.stdout"); |
| | | 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))); |
| | | |
| | | System.setProperty("java.util.logging.config.file", props.getProperty("hsadmin.log.file", System.getProperty("user.home") + "/.hsadmin.log.properties")); |
| | | final QueueServer qServ = new QueueServer(); |
| | | qServ.setJmsFactory(props.getProperty("hsadmin.jms.factory")); |
| | | qServ.setJmsSystemQueue(props.getProperty("hsadmin.jms.system-queue")); |
| | | qServ.setJmsStatusQueue(props.getProperty("hsadmin.jms.status-queue")); |
| | | qServ.setJmsUserName(props.getProperty("hsadmin.jms.username")); |
| | | qServ.setJmsPassWord(props.getProperty("hsadmin.jms.password")); |
| | | qServ.setServiceEMail(props.getProperty("hsadmin.email.service")); |
| | | System.out.println("=================================================="); |
| | | System.out.println("hsadmin-qserv " + VERSION_NO + " started using:"); |
| | | System.out.println("queue server: " + props.getProperty("hsadmin.jms.factory")); |
| | | System.out.println("system queue: " + props.getProperty("hsadmin.jms.system-queue")); |
| | | System.out.println("status queue: " + props.getProperty("hsadmin.jms.status-queue")); |
| | | System.out.println("queue user: " + props.getProperty("hsadmin.jms.username")); |
| | | System.out.println("=================================================="); |
| | | qServ.setServiceEMail(props.getProperty("hsadmin.log.email")); |
| | | Logger logger = Logger.getLogger("de.hsadmin.core.qserv"); |
| | | logger.log(Level.CONFIG, "hsadmin-qserv " + VERSION_NO + " started using:" |
| | | + "\nqueue server: " + props.getProperty("hsadmin.jms.factory") |
| | | + "\nsystem queue: " + props.getProperty("hsadmin.jms.system-queue") |
| | | + "\nstatus queue: " + props.getProperty("hsadmin.jms.status-queue") |
| | | + "\nqueue user: " + props.getProperty("hsadmin.jms.username")); |
| | | Runtime.getRuntime().addShutdownHook(new Thread() { |
| | | @Override |
| | | public void run() { |
| | |
| | | } |
| | | } |
| | | |
| | | private void setServiceEMail(String property) { |
| | | if (property == null) { |
| | | private static String userHelp(File propFile) { |
| | | 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"; |
| | | } else { |
| | | serviceEMail = property; |
| | | serviceEMail = emailAddress; |
| | | } |
| | | } |
| | | |
| | |
| | | receiver.setMessageListener(this); |
| | | return true; |
| | | } catch (Exception e) { |
| | | System.out.println(e.getMessage()); |
| | | logger.log(Level.WARNING, e.getMessage(), e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | public synchronized void onMessage(Message jmsMessage) { |
| | | // TODO logging |
| | | System.out.println("\nonMessage(" + jmsMessage); |
| | | logger.log(Level.INFO, jmsMessage.toString()); |
| | | QueueTask task = null; |
| | | try { |
| | | ObjectMessage jmsObjectMessage = (ObjectMessage) jmsMessage; |
| | | task = (QueueTask) jmsObjectMessage.getObject(); |
| | | Processor processor = task.getProcessor(); |
| | | System.out.println("processing (" + task.getTitle() + " | started(" |
| | | logger.log(Level.INFO, "processing (" + task.getTitle() + " | started(" |
| | | + task.getStarted() + ") |" + task.getDetails() + "|" |
| | | + processor + ")"); |
| | | System.out.println("with " + processor); |
| | | try { |
| | | processor.process(); |
| | | System.out.println("done"); |
| | | logger.log(Level.INFO, "done"); |
| | | } catch (ProcessorException e) { |
| | | logException(e); |
| | | task.setException(e); |
| | |
| | | } |
| | | |
| | | private void logException(Throwable t) { |
| | | System.err.println("exception " + t); // TODO: logging |
| | | t.printStackTrace(System.err); |
| | | if (t.getCause() != null) { |
| | | System.err.println("caused by exception " |
| | | + t.getCause()); // TODO: logging |
| | | t.getCause().printStackTrace(System.err); |
| | | } else |
| | | System.err.println("no further cause available"); |
| | | logger.log(Level.SEVERE, t.getMessage(), t); |
| | | Throwable cause = t.getCause(); |
| | | if (cause != null) { |
| | | logger.log(Level.SEVERE, "cause: " + t.getMessage(), t); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onException(JMSException e) { |
| | | System.out.println("Exception: " + e.getMessage()); |
| | | logger.log(Level.WARNING, e.getMessage(), e); |
| | | close(); |
| | | while (!connect()) { |
| | | try { |
| | |
| | | Session statusSession = null; |
| | | Connection statusConnection = null; |
| | | try { |
| | | System.out.println("sendStatus( " + queueMessage + ")"); |
| | | logger.log(Level.INFO, "sendStatus(" + queueMessage + ")"); |
| | | Context ctx = new InitialContext(); |
| | | System.out.println("Created InitialContext"); |
| | | ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(jmsFactory); |
| | | System.out.println("connectionFactory= " + connectionFactory.toString()); |
| | | Destination queue = (Destination) ctx.lookup(jmsStatusQueue); |
| | | statusConnection = connectionFactory.createConnection(jmsUserName, jmsPassWord); |
| | | statusSession = statusConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| | | producer = statusSession.createProducer(queue); |
| | | ObjectMessage statusMessage = statusSession.createObjectMessage(queueMessage); |
| | | System.out.println("send( " + statusMessage + ")"); |
| | | logger.log(Level.INFO, "send(" + statusMessage + ")"); |
| | | producer.send(statusMessage); |
| | | } catch (Exception statusException) { |
| | | // TODO: log exception |
| | | System.out.println("failed"); |
| | | statusException.printStackTrace(System.err); |
| | | logger.log(Level.SEVERE, statusException.getMessage(), statusException); |
| | | } finally { |
| | | // close JMS |
| | | try { producer.close(); } catch (Exception exc) { } |