HSAdmin Backend Domains, E-Mail, Datenbanken
Peter Hormanns
2016-11-16 672d25dce39552a8f8ba4c5c16730de47ec66b1c
prepare external pac module, jms tasks
1 files added
3 files modified
183 ■■■■■ changed files
hsarback/conf/WEB-INF/dev-web.xml 16 ●●●● patch | view | raw | blame | history
hsarback/conf/WEB-INF/prod-web.xml 17 ●●●●● patch | view | raw | blame | history
hsarback/conf/WEB-INF/test-web.xml 17 ●●●●● patch | view | raw | blame | history
hsarback/src/de/hsadmin/mods/pac/PacTasksServlet.java 133 ●●●●● patch | view | raw | blame | history
hsarback/conf/WEB-INF/dev-web.xml
@@ -14,6 +14,12 @@
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Queue PacTasks Servlet</servlet-name>
        <servlet-class>de.hsadmin.mods.pac.PacTasksServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet>
        <servlet-name>XmlRpcServlet</servlet-name>
        <servlet-class>de.hsadmin.remote.HSXmlRpcServlet</servlet-class>
@@ -29,8 +35,8 @@
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CLI Client Connector</servlet-name>
        <url-pattern>/hsadmin/cli-interface/</url-pattern>
        <servlet-name>Queue PacTasks Servlet</servlet-name>
        <url-pattern>/queuePacTasks</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
@@ -75,5 +81,11 @@
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <res-ref-name>jms/hsadminSystem-pactasks</res-ref-name>
        <res-type>javax.jms.Queue</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
         
</web-app>
hsarback/conf/WEB-INF/prod-web.xml
@@ -22,6 +22,12 @@
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Queue PacTasks Servlet</servlet-name>
        <servlet-class>de.hsadmin.mods.pac.PacTasksServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet>
        <servlet-name>XmlRpcServlet</servlet-name>
        <servlet-class>de.hsadmin.remote.HSXmlRpcServlet</servlet-class>
@@ -34,6 +40,11 @@
    <servlet-mapping>
        <servlet-name>Queue Status Servlet</servlet-name>
        <url-pattern>/queueStatus</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Queue PacTasks Servlet</servlet-name>
        <url-pattern>/queuePacTasks</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
@@ -678,6 +689,12 @@
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <res-ref-name>jms/hsadminSystem-pactasks</res-ref-name>
        <res-type>javax.jms.Queue</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
         
    <security-constraint>
       <web-resource-collection>
hsarback/conf/WEB-INF/test-web.xml
@@ -22,6 +22,12 @@
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Queue PacTasks Servlet</servlet-name>
        <servlet-class>de.hsadmin.mods.pac.PacTasksServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet>
        <servlet-name>XmlRpcServlet</servlet-name>
        <servlet-class>de.hsadmin.remote.HSXmlRpcServlet</servlet-class>
@@ -39,6 +45,11 @@
    <servlet-mapping>
        <servlet-name>XmlRpcServlet</servlet-name>
        <url-pattern>/xmlrpc/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Queue PacTasks Servlet</servlet-name>
        <url-pattern>/queuePacTasks</url-pattern>
    </servlet-mapping>
    <resource-ref>
@@ -78,6 +89,12 @@
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
     <resource-ref>
        <res-ref-name>jms/hsadminSystem-pactasks</res-ref-name>
        <res-type>javax.jms.Queue</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
         
    <security-constraint>
       <web-resource-collection>
hsarback/src/de/hsadmin/mods/pac/PacTasksServlet.java
New file
@@ -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) { }
    }
}