prepare external pac module, jms tasks

This commit is contained in:
Peter Hormanns 2016-11-16 18:00:33 +01:00
parent 77fc48aaa7
commit 672d25dce3
4 changed files with 181 additions and 2 deletions

View File

@ -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>

View File

@ -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>
@ -36,6 +42,11 @@
<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>
<servlet-name>XmlRpcServlet</servlet-name>
<url-pattern>/xmlrpc/*</url-pattern>
@ -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>

View File

@ -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>
@ -41,6 +47,11 @@
<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>
<res-ref-name>jdbc/HSAdminDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
@ -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>

View 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) { }
}
}