From 672d25dce39552a8f8ba4c5c16730de47ec66b1c Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Wed, 16 Nov 2016 18:00:33 +0100 Subject: [PATCH] prepare external pac module, jms tasks --- hsarback/conf/WEB-INF/dev-web.xml | 16 ++- hsarback/conf/WEB-INF/prod-web.xml | 17 +++ hsarback/conf/WEB-INF/test-web.xml | 17 +++ .../de/hsadmin/mods/pac/PacTasksServlet.java | 133 ++++++++++++++++++ 4 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 hsarback/src/de/hsadmin/mods/pac/PacTasksServlet.java diff --git a/hsarback/conf/WEB-INF/dev-web.xml b/hsarback/conf/WEB-INF/dev-web.xml index 18aed7a..6c722bb 100644 --- a/hsarback/conf/WEB-INF/dev-web.xml +++ b/hsarback/conf/WEB-INF/dev-web.xml @@ -14,6 +14,12 @@ 1 + + Queue PacTasks Servlet + de.hsadmin.mods.pac.PacTasksServlet + 1 + + XmlRpcServlet de.hsadmin.remote.HSXmlRpcServlet @@ -29,8 +35,8 @@ - CLI Client Connector - /hsadmin/cli-interface/ + Queue PacTasks Servlet + /queuePacTasks @@ -75,5 +81,11 @@ Container Shareable + + jms/hsadminSystem-pactasks + javax.jms.Queue + Container + Shareable + diff --git a/hsarback/conf/WEB-INF/prod-web.xml b/hsarback/conf/WEB-INF/prod-web.xml index 4899424..2e937c2 100644 --- a/hsarback/conf/WEB-INF/prod-web.xml +++ b/hsarback/conf/WEB-INF/prod-web.xml @@ -22,6 +22,12 @@ 1 + + Queue PacTasks Servlet + de.hsadmin.mods.pac.PacTasksServlet + 1 + + XmlRpcServlet de.hsadmin.remote.HSXmlRpcServlet @@ -36,6 +42,11 @@ /queueStatus + + Queue PacTasks Servlet + /queuePacTasks + + XmlRpcServlet /xmlrpc/* @@ -678,6 +689,12 @@ Container Shareable + + jms/hsadminSystem-pactasks + javax.jms.Queue + Container + Shareable + diff --git a/hsarback/conf/WEB-INF/test-web.xml b/hsarback/conf/WEB-INF/test-web.xml index 47c2dd5..cb180a5 100644 --- a/hsarback/conf/WEB-INF/test-web.xml +++ b/hsarback/conf/WEB-INF/test-web.xml @@ -22,6 +22,12 @@ 1 + + Queue PacTasks Servlet + de.hsadmin.mods.pac.PacTasksServlet + 1 + + XmlRpcServlet de.hsadmin.remote.HSXmlRpcServlet @@ -41,6 +47,11 @@ /xmlrpc/* + + Queue PacTasks Servlet + /queuePacTasks + + jdbc/HSAdminDB javax.sql.DataSource @@ -78,6 +89,12 @@ Container Shareable + + jms/hsadminSystem-pactasks + javax.jms.Queue + Container + Shareable + diff --git a/hsarback/src/de/hsadmin/mods/pac/PacTasksServlet.java b/hsarback/src/de/hsadmin/mods/pac/PacTasksServlet.java new file mode 100644 index 0000000..ecc9771 --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/pac/PacTasksServlet.java @@ -0,0 +1,133 @@ +package de.hsadmin.mods.pac; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.jms.ExceptionListener; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.Queue; +import javax.jms.QueueConnection; +import javax.jms.QueueConnectionFactory; +import javax.jms.QueueReceiver; +import javax.jms.QueueSession; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import de.hsadmin.core.model.TechnicalException; +import de.hsadmin.core.model.Transaction; +import de.hsadmin.core.util.Config; + +public class PacTasksServlet extends HttpServlet + implements MessageListener, ExceptionListener { + + private static final long serialVersionUID = -5701350884034782083L; + + private String jmsUser; + private String jmsPass; + private QueueConnectionFactory queueConnectionFactory; + private QueueConnection queueConnection; + private QueueSession queueSession; + private boolean isConnected; + private int messageCount; + private int errorCount; + + @Override + public void init() throws ServletException { + isConnected = false; + messageCount = 0; + errorCount = 0; + try { + connect(); + } catch (NamingException e) { + throw new ServletException(e); + } + } + + private void connect() throws NamingException { + Config config = Config.getInstance(); + jmsUser = config.getProperty("hsadmin.jms.username", "hsadmin"); + jmsPass = config.getProperty("hsadmin.jms.password", "hsadmin-pw"); + InitialContext ctx = new InitialContext(); + Context env = (Context) ctx.lookup("java:comp/env"); + queueConnectionFactory = (QueueConnectionFactory) env.lookup("jms/QueueCF"); + while (!isConnected) { + try { + queueConnection = queueConnectionFactory.createQueueConnection(jmsUser, jmsPass); + queueConnection.setExceptionListener(this); + queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); + Queue queue = (Queue) env.lookup("jms/hsadminSystem-pactasks"); + queueConnection.start(); + QueueReceiver receiver = queueSession.createReceiver(queue); + receiver.setMessageListener(this); + isConnected = true; + } catch (JMSException e) { + close(); + try { + Thread.sleep(2000); + } catch (InterruptedException e1) { } + } + } + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + resp.setContentType("text/plain"); + PrintWriter writer = resp.getWriter(); + writer.println("Verbindungsstatus: " + (isConnected ? "OK" : "ERROR")); + writer.println("Verarbeitete Nachrichten: " + messageCount); + writer.println("Verarbeitungsfehler: " + errorCount); + writer.close(); + } + + @Override + public void destroy() { + close(); + } + + private void close() { + try { if (queueSession != null) queueSession.close(); } catch (JMSException e) { } + try { if (queueConnection != null) queueConnection.close(); } catch (JMSException e) { } + isConnected = false; + } + + @Override + public void onMessage(Message jmsMessage) { + Transaction transaction = null; + messageCount++; + try { + final TextMessage txtMessage = (TextMessage) jmsMessage; + System.out.println(txtMessage.getText()); +// transaction = new Transaction("pactasks"); +// transaction.beginTransaction(); +// EntityManager em = transaction.getEntityManager(); +// em.clear(); +// em.persist(persistentQT); +// em.flush(); +// transaction.commitTransaction(); + } catch (Exception e) { + errorCount++; +// if (transaction != null) transaction.rollbackTransaction(); + throw new TechnicalException(e); + } finally { + if (transaction != null) transaction.close(); + } + } + + @Override + public void onException(JMSException exception) { + close(); + try { Thread.sleep(10000); } catch (InterruptedException e) { } + try { connect(); } catch (NamingException e) { } + } + +}