remove jndi config, new ValueObject TaskTransfer, v4.0.16

This commit is contained in:
Peter Hormanns 2023-08-14 20:16:47 +02:00
parent 8409ede74b
commit a85f0d1bec
18 changed files with 299 additions and 330 deletions

View File

@ -5,7 +5,7 @@
<groupId>de.hsadmin</groupId> <groupId>de.hsadmin</groupId>
<artifactId>hsar</artifactId> <artifactId>hsar</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>4.0.15</version> <version>4.0.16</version>
<name>HSAdmin Stable Backend Webapp</name> <name>HSAdmin Stable Backend Webapp</name>
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<properties> <properties>
@ -36,12 +36,12 @@
<dependency> <dependency>
<groupId>de.hsadmin.core</groupId> <groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId> <artifactId>hsadmin-util</artifactId>
<version>4.0.15</version> <version>4.0.16</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>de.hsadmin.core</groupId> <groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv</artifactId> <artifactId>hsadmin-qserv</artifactId>
<version>4.0.15</version> <version>4.0.16</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-httpclient</groupId> <groupId>commons-httpclient</groupId>
@ -78,6 +78,11 @@
<artifactId>openjpa</artifactId> <artifactId>openjpa</artifactId>
<version>3.2.2</version> <version>3.2.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.17.5</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -90,18 +95,6 @@
<version>4.0.1</version> <version>4.0.1</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.18.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>hsar</finalName> <finalName>hsar</finalName>

View File

@ -2,6 +2,8 @@ package de.hsadmin.servlets;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Date;
import javax.jms.ExceptionListener; import javax.jms.ExceptionListener;
import javax.jms.JMSException; import javax.jms.JMSException;
@ -10,15 +12,9 @@ import javax.jms.MessageListener;
import javax.jms.ObjectMessage; import javax.jms.ObjectMessage;
import javax.jms.Queue; import javax.jms.Queue;
import javax.jms.QueueConnection; import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver; import javax.jms.QueueReceiver;
import javax.jms.QueueSession; import javax.jms.QueueSession;
import javax.jms.Session; import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -31,9 +27,9 @@ import org.apache.activemq.ActiveMQConnectionFactory;
import de.hsadmin.core.model.TechnicalException; import de.hsadmin.core.model.TechnicalException;
import de.hsadmin.core.model.TicketValidator; import de.hsadmin.core.model.TicketValidator;
import de.hsadmin.core.model.Transaction; import de.hsadmin.core.model.Transaction;
import de.hsadmin.core.qserv.NullProcessor;
import de.hsadmin.core.qserv.Processor; import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.QueueTask; import de.hsadmin.core.qserv.QueueTask;
import de.hsadmin.core.qserv.TaskTransfer;
import de.hsadmin.core.util.Config; import de.hsadmin.core.util.Config;
public class QueueStatusReceiverServlet extends HttpServlet public class QueueStatusReceiverServlet extends HttpServlet
@ -41,11 +37,10 @@ public class QueueStatusReceiverServlet extends HttpServlet
private static final long serialVersionUID = -5701350884034782083L; private static final long serialVersionUID = -5701350884034782083L;
private static boolean initQueuesDone = false; private String jmsUrl;
private String jmsUser; private String jmsUser;
private String jmsPass; private String jmsPass;
private QueueConnectionFactory queueConnectionFactory; private String jmsStatusQueue;
private QueueConnection queueConnection; private QueueConnection queueConnection;
private QueueSession queueSession; private QueueSession queueSession;
private boolean isConnected; private boolean isConnected;
@ -61,39 +56,24 @@ public class QueueStatusReceiverServlet extends HttpServlet
isConnected = false; isConnected = false;
messageCount = 0; messageCount = 0;
errorCount = 0; errorCount = 0;
try {
if (!initQueuesDone) {
initQueues();
}
initQueuesDone = true;
} catch (NamingException e) {
throw new ServletException(e);
}
try {
connect(); connect();
} catch (NamingException e) {
throw new ServletException(e);
}
} }
private void connect() throws NamingException { private void connect() {
Config config = Config.getInstance(); Config config = Config.getInstance();
jmsUrl = config.getProperty("hsadmin.jms.url", "tcp://localhost:61616");
jmsUser = config.getProperty("hsadmin.jms.username", "hsadmin"); jmsUser = config.getProperty("hsadmin.jms.username", "hsadmin");
jmsPass = config.getProperty("hsadmin.jms.password", "hsadmin-pw"); jmsPass = config.getProperty("hsadmin.jms.password", "hsadmin-pw");
InitialContext ctx = new InitialContext(); jmsStatusQueue = config.getProperty("hsadmin.jms.status-queue", "queue.Status");
Context env = (Context) ctx.lookup("java:comp/env"); final ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(jmsUrl);
queueConnectionFactory = (QueueConnectionFactory) env.lookup("jms/QueueCF"); mqConnectionFactory.setTrustAllPackages(true);
if (queueConnectionFactory instanceof ActiveMQConnectionFactory) {
ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) queueConnectionFactory;
activeMQConnectionFactory.setTrustAllPackages(true);
}
int timeoutCounter = 10; int timeoutCounter = 10;
while (!isConnected && (timeoutCounter > 0)) { while (!isConnected && (timeoutCounter > 0)) {
try { try {
queueConnection = queueConnectionFactory.createQueueConnection(jmsUser, jmsPass); queueConnection = mqConnectionFactory.createQueueConnection(jmsUser, jmsPass);
queueConnection.setExceptionListener(this); queueConnection.setExceptionListener(this);
queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) env.lookup("jms/hsadminStatus"); Queue queue = queueSession.createQueue(jmsStatusQueue);
queueConnection.start(); queueConnection.start();
QueueReceiver receiver = queueSession.createReceiver(queue); QueueReceiver receiver = queueSession.createReceiver(queue);
receiver.setMessageListener(this); receiver.setMessageListener(this);
@ -108,29 +88,6 @@ public class QueueStatusReceiverServlet extends HttpServlet
} }
} }
private void initQueues() throws NamingException {
InitialContext ctx = new InitialContext();
NamingEnumeration<NameClassPair> list = ctx.list("java:comp/env/jms");
Transaction transaction = new Transaction("anonymous");
transaction.beginTransaction();
EntityManager entityManager = transaction.getEntityManager();
while (list.hasMore()) {
NameClassPair pair = list.next();
String jndiName = pair.getName();
if (jndiName != null && jndiName.startsWith("hsadminSystem-")) {
QueueTask task = new QueueTask();
task.setProcessor(new NullProcessor());
entityManager.persist(task);
entityManager.flush();
String hive = jndiName.substring(14);
transaction.enqueue(hive, task);
}
}
transaction.commitTransaction();
transaction.close();
ctx.close();
}
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { throws ServletException, IOException {
@ -159,7 +116,9 @@ public class QueueStatusReceiverServlet extends HttpServlet
messageCount++; messageCount++;
try { try {
ObjectMessage objMessage = (ObjectMessage) jmsMessage; ObjectMessage objMessage = (ObjectMessage) jmsMessage;
QueueTask detachedQT = (QueueTask) objMessage.getObject(); final Serializable object = objMessage.getObject();
if (object != null) {
TaskTransfer detachedQT = (TaskTransfer) object;
transaction = new Transaction("statusreceiver"); transaction = new Transaction("statusreceiver");
transaction.beginTransaction(); transaction.beginTransaction();
EntityManager em = transaction.getEntityManager(); EntityManager em = transaction.getEntityManager();
@ -173,6 +132,11 @@ public class QueueStatusReceiverServlet extends HttpServlet
em.persist(persistentQT); em.persist(persistentQT);
em.flush(); em.flush();
transaction.commitTransaction(); transaction.commitTransaction();
} else {
final String err = "Deserialization failed " + new Date();
System.out.println(err);
throw new TechnicalException(err);
}
} catch (Exception e) { } catch (Exception e) {
errorCount++; errorCount++;
if (transaction != null) transaction.rollbackTransaction(); if (transaction != null) transaction.rollbackTransaction();
@ -186,7 +150,7 @@ public class QueueStatusReceiverServlet extends HttpServlet
public void onException(JMSException exception) { public void onException(JMSException exception) {
close(); close();
try { Thread.sleep(10000); } catch (InterruptedException e) { } try { Thread.sleep(10000); } catch (InterruptedException e) { }
try { connect(); } catch (NamingException e) { } connect();
} }
} }

View File

@ -5,29 +5,4 @@
global="jdbc/HSAdminDB" global="jdbc/HSAdminDB"
type="javax.sql.DataSource"/> type="javax.sql.DataSource"/>
<ResourceLink
name="jms/QueueCF"
global="jms/QueueCF"
type="javax.jms.QueueConnectionFactory"/>
<ResourceLink
name="jms/hsadminSystem-h99"
global="jms/hsadminSystem-h99"
type="javax.jms.Queue"/>
<ResourceLink
name="jms/hsadminSystem-testmail"
global="jms/hsadminSystem-testmail"
type="javax.jms.Queue"/>
<ResourceLink
name="jms/hsadminSystem-testdns"
global="jms/hsadminSystem-testdns"
type="javax.jms.Queue"/>
<ResourceLink
name="jms/hsadminStatus"
global="jms/hsadminStatus"
type="javax.jms.Queue"/>
</Context> </Context>

View File

@ -57,36 +57,5 @@
<res-auth>Container</res-auth> <res-auth>Container</res-auth>
</resource-ref> </resource-ref>
<resource-ref>
<res-ref-name>jms/QueueCF</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/hsadminSystem-h99</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/hsadminSystem-testdns</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/hsadminSystem-testmail</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/hsadminStatus</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> </web-app>

2
qserv/.gitignore vendored
View File

@ -4,4 +4,4 @@
.classpath .classpath
.project .project
.settings/ .settings/
hsadmin.properties

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.hsadmin.core</groupId> <groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv</artifactId> <artifactId>hsadmin-qserv</artifactId>
<version>4.0.15</version> <version>4.0.16</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version> <java.version>11</java.version>
@ -13,7 +13,7 @@
<dependency> <dependency>
<groupId>de.hsadmin.core</groupId> <groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId> <artifactId>hsadmin-util</artifactId>
<version>4.0.15</version> <version>4.0.16</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>

View File

@ -14,6 +14,7 @@ import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.NullProcessor; import de.hsadmin.core.qserv.NullProcessor;
import de.hsadmin.core.qserv.Processor; import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.QueueTask; import de.hsadmin.core.qserv.QueueTask;
import de.hsadmin.core.qserv.TaskTransfer;
import de.hsadmin.core.util.HSAdminException; import de.hsadmin.core.util.HSAdminException;
import de.hsadmin.mods.user.UnixUser; import de.hsadmin.mods.user.UnixUser;
@ -199,8 +200,10 @@ public abstract class AbstractModuleImpl implements ModuleInterface {
StringBuilder details = new StringBuilder(); StringBuilder details = new StringBuilder();
String title = entityTypeName + " (" + entity.createStringKey() + ") " + action; String title = entityTypeName + " (" + entity.createStringKey() + ") " + action;
QueueTask task = new QueueTask(user, title, details.toString(), proc); QueueTask task = new QueueTask(user, title, details.toString(), proc);
transaction.getEntityManager().persist(task); final EntityManager entityManager = transaction.getEntityManager();
transaction.enqueue(entity.getHiveName(), task); entityManager.persist(task);
entityManager.refresh(task);
transaction.enqueue(entity.getHiveName(), TaskTransfer.getTransferObject(task));
} }
public String toString(StackTraceElement[] stackTrace) { public String toString(StackTraceElement[] stackTrace) {

View File

@ -6,11 +6,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.jms.JMSException;
import javax.jms.Queue; import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory; import javax.jms.QueueConnectionFactory;
import javax.naming.Context; import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
import javax.persistence.Query; import javax.persistence.Query;
@ -19,7 +19,7 @@ import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManager;
import de.hsadmin.core.qserv.QueueClient; import de.hsadmin.core.qserv.QueueClient;
import de.hsadmin.core.qserv.QueueTask; import de.hsadmin.core.qserv.TaskTransfer;
import de.hsadmin.core.util.Config; import de.hsadmin.core.util.Config;
import de.hsadmin.mods.cust.Customer; import de.hsadmin.mods.cust.Customer;
import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.pac.Pac;
@ -32,24 +32,23 @@ public class Transaction {
private String loginName; private String loginName;
private Map<String, QueueTaskStore> taskStores; private Map<String, QueueTaskStore> taskStores;
private boolean transactionActive; private boolean transactionActive;
private InitialContext ctx; private Config config;
private String jmsServerUrl;
private String jmsUsername;
private String jmsPassword;
public Transaction(String loginName) { public Transaction(String loginName) {
this.config = Config.getInstance();
this.jmsServerUrl = config.getProperty("hsadmin.jms.url", "tcp://localhost:61616");
this.jmsUsername = config.getProperty("hsadmin.jms.username", "hsar");
this.jmsPassword = config.getProperty("hsadmin.jms.password", "default");
this.transactionActive = false; this.transactionActive = false;
this.entityManager = PersistenceManager.getEntityManager("hsadmin"); this.entityManager = PersistenceManager.getEntityManager("hsadmin");
this.loginName = loginName; this.loginName = loginName;
this.taskStores = new HashMap<String, QueueTaskStore>(); this.taskStores = new HashMap<String, QueueTaskStore>();
try { final ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(jmsServerUrl);
ctx = new InitialContext(); mqConnectionFactory.setTrustAllPackages(true);
Context env = (Context) ctx.lookup("java:comp/env"); this.queueConnectionFactory = mqConnectionFactory;
queueConnectionFactory = (QueueConnectionFactory) env.lookup("jms/QueueCF");
if (queueConnectionFactory instanceof ActiveMQConnectionFactory) {
ActiveMQConnectionFactory activeMQconnectionFatory = (ActiveMQConnectionFactory) queueConnectionFactory;
activeMQconnectionFatory.setTrustAllPackages(true);
}
} catch (NamingException e) {
throw new TechnicalException("no jms queue: jms/QueueCF", e);
}
} }
public EntityManager getEntityManager() { public EntityManager getEntityManager() {
@ -61,16 +60,15 @@ public class Transaction {
} }
public Queue lookupJMSQueue(String queueName) { public Queue lookupJMSQueue(String queueName) {
if (ctx != null) {
try { try {
Context env = (Context) ctx.lookup("java:comp/env"); final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(jmsUsername, jmsPassword);
return (Queue) env.lookup("jms/" + queueName); final QueueSession session = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
} catch (NamingException e) { final Queue queue = session.createQueue(queueName);
return queue;
} catch (JMSException e) {
throw new TechnicalException("no jms queue: jms/" + queueName, e); throw new TechnicalException("no jms queue: jms/" + queueName, e);
} }
} }
return null;
}
public String getLoginName() { public String getLoginName() {
if (loginName != null) { if (loginName != null) {
@ -79,7 +77,7 @@ public class Transaction {
throw new TechnicalException("no login"); throw new TechnicalException("no login");
} }
public void enqueue(String hiveName, QueueTask task) { public void enqueue(String hiveName, TaskTransfer task) {
QueueTaskStore taskStore = taskStores.get(hiveName); QueueTaskStore taskStore = taskStores.get(hiveName);
if (taskStore == null) { if (taskStore == null) {
taskStore = new QueueTaskStore(); taskStore = new QueueTaskStore();
@ -96,8 +94,8 @@ public class Transaction {
QueueClient qClient = null; QueueClient qClient = null;
try { try {
qClient = new QueueClient(queueConnectionFactory, jmsSystemQueue); qClient = new QueueClient(queueConnectionFactory, jmsSystemQueue);
for (QueueTask task : store.getTasks()) { for (TaskTransfer taskTransfer : store.getTasks()) {
qClient.send(task); qClient.send(taskTransfer);
} }
} catch (Exception e) { } catch (Exception e) {
throw new TechnicalException(e); throw new TechnicalException(e);
@ -151,7 +149,7 @@ public class Transaction {
} }
/** /**
* Detach entities from hibernate session. * Detach entities from JPA session.
* Used to detach entities before update. Makes it possible to compare * Used to detach entities before update. Makes it possible to compare
* old and new attribute values. * old and new attribute values.
*/ */
@ -163,17 +161,17 @@ public class Transaction {
} }
class QueueTaskStore { class QueueTaskStore {
private List<QueueTask> taskList; private List<TaskTransfer> taskList;
QueueTaskStore() { QueueTaskStore() {
taskList = new ArrayList<QueueTask>(); taskList = new ArrayList<TaskTransfer>();
} }
public void clear() { public void clear() {
taskList = new ArrayList<QueueTask>(); taskList = new ArrayList<TaskTransfer>();
} }
void add(QueueTask t) { void add(TaskTransfer t) {
taskList.add(t); taskList.add(t);
} }
Iterable<QueueTask> getTasks() { Iterable<TaskTransfer> getTasks() {
return taskList; return taskList;
} }
} }

View File

@ -14,4 +14,9 @@ abstract public class AbstractProcessor implements Processor {
task.done(); task.done();
} }
@Override
public String toString() {
return "a Processor of class " + this.getClass().getCanonicalName();
}
} }

View File

@ -3,7 +3,7 @@ package de.hsadmin.core.qserv;
public class NullProcessor extends AbstractProcessor { public class NullProcessor extends AbstractProcessor {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = -3667951484545665538L;
@Override @Override
public Object process() throws ProcessorException { public Object process() throws ProcessorException {

View File

@ -18,7 +18,7 @@ import de.hsadmin.core.util.Config;
* *
* @author mi * @author mi
*/ */
public class QueueClient extends QueueCommons { public class QueueClient {
private QueueConnectionFactory jmsConnectionFactory; private QueueConnectionFactory jmsConnectionFactory;
private Queue jmsSystemQueue; private Queue jmsSystemQueue;
@ -36,11 +36,11 @@ public class QueueClient extends QueueCommons {
String jmsUser = config.getProperty("hsadmin.jms.username", "hsadmin"); String jmsUser = config.getProperty("hsadmin.jms.username", "hsadmin");
String jmsPass = config.getProperty("hsadmin.jms.password", "hsadmin-pw"); String jmsPass = config.getProperty("hsadmin.jms.password", "hsadmin-pw");
jmsConnection = jmsConnectionFactory.createQueueConnection(jmsUser, jmsPass); jmsConnection = jmsConnectionFactory.createQueueConnection(jmsUser, jmsPass);
jmsSession = jmsConnection.createQueueSession(DEFAULT, Session.AUTO_ACKNOWLEDGE); jmsSession = jmsConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
jmsSender = jmsSession.createSender(jmsSystemQueue); jmsSender = jmsSession.createSender(jmsSystemQueue);
} }
public void send(QueueTask task) throws ProcessorException { public void send(TaskTransfer task) throws ProcessorException {
try { try {
ObjectMessage jmsMessage = jmsSession.createObjectMessage(task); ObjectMessage jmsMessage = jmsSession.createObjectMessage(task);
jmsSender.send(jmsMessage); jmsSender.send(jmsMessage);

View File

@ -1,14 +0,0 @@
package de.hsadmin.core.qserv;
public class QueueCommons
{
protected static final boolean TRANSACTED = true;
protected static final boolean IMMEDIATE = false;
protected static final boolean DEFAULT = IMMEDIATE;
public QueueCommons()
{
super();
}
}

View File

@ -1,13 +1,8 @@
package de.hsadmin.core.qserv; package de.hsadmin.core.qserv;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener; import javax.jms.ExceptionListener;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
@ -16,68 +11,49 @@ import javax.jms.MessageProducer;
import javax.jms.ObjectMessage; import javax.jms.ObjectMessage;
import javax.jms.Queue; import javax.jms.Queue;
import javax.jms.QueueConnection; import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver; import javax.jms.QueueReceiver;
import javax.jms.QueueSession; import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
public class QueueServer extends QueueCommons implements MessageListener, ExceptionListener { import de.hsadmin.core.util.Config;
private static final String VERSION_NO = "4.0.11"; public class QueueServer implements MessageListener, ExceptionListener {
private static final String VERSION_NO = "4.0.16";
private Logger logger; private Logger logger;
private QueueConnection conn;
private QueueSession queueSession; private String jmsUrl;
private String jmsStatusQueue; private String jmsUsername;
private String jmsPassWord; private String jmsPassword;
private String jmsUserName;
private String jmsSystemQueue; private String jmsSystemQueue;
private String jmsFactory; private String jmsStatusQueue;
private String serviceEMail; private String serviceEMail;
private String fromEMail; private String fromEMail;
private QueueConnection queueConnection;
/** /**
* 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"); final Config config = Config.getInstance();
if (args.length == 1) { System.setProperty("java.util.logging.config.file", config.getProperty("hsadmin.log.config", System.getProperty("user.home") + "/.hsadmin.log.properties"));
propFile = new File(args[0]);
} else {
if (args.length != 0) {
throw new Exception(userHelp(propFile));
}
}
FileInputStream propStream = null;
try {
propStream = new FileInputStream(propFile);
} catch (Exception e) {
System.out.println("couldn't read config file " + propFile.getAbsolutePath());
System.exit(1);
}
Properties props = new Properties(System.getProperties());
props.load(propStream);
propStream.close();
System.setProperty("java.util.logging.config.file", props.getProperty("hsadmin.log.config", System.getProperty("user.home") + "/.hsadmin.log.properties"));
final QueueServer qServ = new QueueServer(); final QueueServer qServ = new QueueServer();
qServ.setJmsFactory(props.getProperty("hsadmin.jms.factory")); qServ.setJmsURL(config.getProperty("hsadmin.jms.url"));
qServ.setJmsSystemQueue(props.getProperty("hsadmin.jms.system-queue")); qServ.setJmsSystemQueue(config.getProperty("hsadmin.jms.system-queue"));
qServ.setJmsStatusQueue(props.getProperty("hsadmin.jms.status-queue")); qServ.setJmsStatusQueue(config.getProperty("hsadmin.jms.status-queue"));
qServ.setJmsUserName(props.getProperty("hsadmin.jms.username")); qServ.setJmsUserName(config.getProperty("hsadmin.jms.username"));
qServ.setJmsPassWord(props.getProperty("hsadmin.jms.password")); qServ.setJmsPassWord(config.getProperty("hsadmin.jms.password"));
qServ.setServiceEMail(props.getProperty("hsadmin.log.email")); qServ.setServiceEMail(config.getProperty("hsadmin.log.email"));
qServ.setFromEMail(props.getProperty("hsadmin.log.from")); qServ.setFromEMail(config.getProperty("hsadmin.log.from"));
Logger logger = Logger.getLogger("de.hsadmin.core.qserv"); Logger logger = Logger.getLogger("de.hsadmin.core.qserv");
logger.log(Level.CONFIG, "hsadmin-qserv " + VERSION_NO + " started using:" logger.log(Level.CONFIG, "hsadmin-qserv " + VERSION_NO + " started using:"
+ "\nqueue server: " + props.getProperty("hsadmin.jms.factory") + "\nsystem queue: " + config.getProperty("hsadmin.jms.system-queue")
+ "\nsystem queue: " + props.getProperty("hsadmin.jms.system-queue") + "\nstatus queue: " + config.getProperty("hsadmin.jms.status-queue")
+ "\nstatus queue: " + props.getProperty("hsadmin.jms.status-queue") + "\nqueue user: " + config.getProperty("hsadmin.jms.username"));
+ "\nqueue user: " + props.getProperty("hsadmin.jms.username"));
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
@Override @Override
public void run() { public void run() {
@ -92,18 +68,14 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
} }
} }
private static String userHelp(File propFile) { protected void close() {
return "Wrong number of arguments.\n" if (queueConnection != null) {
+ "With no arguments '" try {
+ propFile queueConnection.close();
+ "' will be used as config file.\n" } catch (JMSException e) {
+ "Or give a properties file as single argument.\n\n" logger.log(Level.SEVERE, e.getMessage());
+ "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() { public QueueServer() {
@ -127,20 +99,15 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
} }
private boolean connect() { private boolean connect() {
// create JMS connection and session
try { try {
Context ctx = new InitialContext(); final ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(jmsUrl);
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx.lookup(jmsFactory); mqConnectionFactory.setTrustAllPackages(true);
if (connectionFactory instanceof ActiveMQConnectionFactory) { queueConnection = mqConnectionFactory.createQueueConnection(jmsUsername, jmsPassword);
ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) connectionFactory; queueConnection.setExceptionListener(this);
activeMQConnectionFactory.setTrustAllPackages(true); final QueueSession session = queueConnection.createQueueSession(false, QueueSession.CLIENT_ACKNOWLEDGE);
} Queue queue = session.createQueue(jmsSystemQueue);
conn = connectionFactory.createQueueConnection(jmsUserName, jmsPassWord); queueConnection.start();
conn.setExceptionListener(this); final QueueReceiver receiver = session.createReceiver(queue);
queueSession = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup(jmsSystemQueue);
conn.start();
QueueReceiver receiver = queueSession.createReceiver(queue);
receiver.setMessageListener(this); receiver.setMessageListener(this);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
@ -151,10 +118,10 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
public synchronized void onMessage(Message jmsMessage) { public synchronized void onMessage(Message jmsMessage) {
logger.log(Level.INFO, jmsMessage.toString()); logger.log(Level.INFO, jmsMessage.toString());
QueueTask task = null; TaskTransfer task = null;
try { try {
ObjectMessage jmsObjectMessage = (ObjectMessage) jmsMessage; ObjectMessage jmsObjectMessage = (ObjectMessage) jmsMessage;
task = (QueueTask) jmsObjectMessage.getObject(); task = (TaskTransfer) jmsObjectMessage.getObject();
Processor processor = task.getProcessor(); Processor processor = task.getProcessor();
logger.log(Level.INFO, "processing (" + task.getTitle() + " | started(" logger.log(Level.INFO, "processing (" + task.getTitle() + " | started("
+ task.getStarted() + ") |" + task.getDetails() + "|" + task.getStarted() + ") |" + task.getDetails() + "|"
@ -164,12 +131,12 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
logger.log(Level.INFO, "done"); logger.log(Level.INFO, "done");
} catch (ProcessorException e) { } catch (ProcessorException e) {
logException(e); logException(e);
task.setException(e); task.setException(e.getMessage());
SmtpHelper.send(fromEMail, serviceEMail, jmsSystemQueue, processor.logInfo()); SmtpHelper.send(fromEMail, serviceEMail, jmsSystemQueue, processor.logInfo());
} }
} catch (Throwable throwable) { } catch (Throwable throwable) {
logException(throwable); logException(throwable);
task.setException(throwable); task.setException(throwable.getMessage());
} finally { } finally {
sendStatus(task); sendStatus(task);
notifyAll(); notifyAll();
@ -187,7 +154,6 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
@Override @Override
public void onException(JMSException e) { public void onException(JMSException e) {
logger.log(Level.WARNING, e.getMessage(), e); logger.log(Level.WARNING, e.getMessage(), e);
close();
while (!connect()) { while (!connect()) {
try { try {
Thread.sleep(10000); Thread.sleep(10000);
@ -195,67 +161,44 @@ public class QueueServer extends QueueCommons implements MessageListener, Except
} }
} }
public void close() { protected void sendStatus(TaskTransfer queueMessage) {
if (queueSession != null) { final ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(jmsUrl);
try { mqConnectionFactory.setTrustAllPackages(true);
queueSession.close();
} catch (JMSException e1) { }
}
if (conn != null) {
try {
conn.close();
} catch (JMSException e1) { }
}
}
protected void sendStatus(QueueTask queueMessage) {
MessageProducer producer = null; MessageProducer producer = null;
Session statusSession = null; QueueSession session = null;
Connection statusConnection = null; try (QueueConnection queueConnection = mqConnectionFactory.createQueueConnection(jmsUsername, jmsPassword)) {
try { queueConnection.setExceptionListener(this);
logger.log(Level.INFO, "sendStatus(" + queueMessage + ")"); session = queueConnection.createQueueSession(false, QueueSession.CLIENT_ACKNOWLEDGE);
Context ctx = new InitialContext(); final Queue queue = session.createQueue(jmsStatusQueue);
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx.lookup(jmsFactory); queueConnection.start();
if (connectionFactory instanceof ActiveMQConnectionFactory) { producer = session.createProducer(queue);
ActiveMQConnectionFactory activeMQConnectionFactory = (ActiveMQConnectionFactory) connectionFactory; final ObjectMessage statusMessage = session.createObjectMessage(queueMessage);
activeMQConnectionFactory.setTrustAllPackages(true);
}
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);
logger.log(Level.INFO, "send(" + statusMessage + ")"); logger.log(Level.INFO, "send(" + statusMessage + ")");
producer.send(statusMessage); producer.send(statusMessage);
} catch (Exception statusException) { } catch (Exception statusException) {
logger.log(Level.SEVERE, statusException.getMessage(), statusException); logger.log(Level.SEVERE, statusException.getMessage(), statusException);
} finally {
// close JMS
try { producer.close(); } catch (Exception exc) { }
try { statusSession.close(); } catch (Exception exc) { }
try { statusConnection.close(); } catch (Exception exc) { }
} }
} }
public void setJmsStatusQueue(String property) { private void setJmsURL(String property) {
jmsStatusQueue = property; jmsUrl = property;
} }
public void setJmsPassWord(String property) { public void setJmsPassWord(String property) {
jmsPassWord = property; jmsPassword = property;
} }
public void setJmsUserName(String property) { public void setJmsUserName(String property) {
jmsUserName = property; jmsUsername = property;
} }
public void setJmsSystemQueue(String property) { public void setJmsSystemQueue(String property) {
jmsSystemQueue = property; jmsSystemQueue = property;
} }
public void setJmsFactory(String property) { public void setJmsStatusQueue(String property) {
jmsFactory = property; jmsStatusQueue = property;
} }
} }

View File

@ -223,14 +223,13 @@ public class QueueTask extends AbstractEntity implements Serializable {
* assigns all data field of qt to this instance, with * assigns all data field of qt to this instance, with
* the exception of the id * the exception of the id
*/ */
public void assign(QueueTask qt) { public void assign(TaskTransfer qt) {
this.user = qt.user; this.title = qt.getTitle();
this.title = qt.title; this.details = qt.getDetails();
this.details = qt.details; this.started = qt.getStarted();
this.started = qt.started; this.finished = qt.getFinished();
this.finished = qt.finished; this.proc = qt.getProcessor();
this.proc = qt.proc; this.exception = qt.getException();
this.exception = qt.exception;
} }
@Override @Override

View File

@ -8,7 +8,7 @@ package de.hsadmin.core.qserv;
*/ */
public class ShellProcessor extends AbstractProcessor { public class ShellProcessor extends AbstractProcessor {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = -8340381162948081669L;
private String aSystemCall; private String aSystemCall;
private String aInput; private String aInput;

View File

@ -0,0 +1,130 @@
package de.hsadmin.core.qserv;
import java.io.Serializable;
import java.util.Date;
import de.hsadmin.mods.user.UnixUser;
public class TaskTransfer implements Serializable {
private static final long serialVersionUID = -2120247737335542484L;
private long id;
private String unixuser;
private Date started;
private Date finished;
private String title;
private String details;
private Processor proc;
private String exception;
public TaskTransfer() {
id = 0L;
unixuser = "anonymous";
started = new Date();
finished = null;
title = "untitled";
details = "none";
setProcessor(new NullProcessor());
setException("");
}
public TaskTransfer(long id, String unixuser, Date started, Date finished, String title, String details, Processor proc, String exception) {
this.id = id;
this.unixuser = unixuser;
this.started = started;
this.finished = finished;
this.title = title;
this.details = details;
this.setProcessor(proc);
this.setException(exception);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUnixuser() {
return unixuser;
}
public void setUnixuser(String unixuser) {
this.unixuser = unixuser;
}
public Date getStarted() {
return started;
}
public void setStarted(Date started) {
this.started = started;
}
public Date getFinished() {
return finished;
}
public void setFinished(Date finished) {
this.finished = finished;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public Processor getProcessor() {
return proc;
}
public void setProcessor(Processor proc) {
this.proc = proc;
}
public String getException() {
return exception;
}
public void setException(String exception) {
this.exception = exception;
}
@Override
public String toString() {
return Long.toString(id) + "|" + title + "|" + started.toString();
}
public static TaskTransfer getTransferObject(QueueTask task) {
long id = task.getId();
final UnixUser user = task.getUser();
String username = "anonymous";
if (user == null) {
username = "anonymous";
} else {
username = user.getName();
}
Date started = task.getStarted();
Date finished = task.getFinished();
String title = task.getTitle();
String details = task.getDetails();
Processor proc = task.getProcessor();
String exception = task.getException();
return new TaskTransfer(id, username, started, finished, title, details, proc, exception);
}
}

View File

@ -4,6 +4,8 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager;
import de.hsadmin.core.model.Transaction; import de.hsadmin.core.model.Transaction;
public class WaitingTasksProcessor extends AbstractProcessor { public class WaitingTasksProcessor extends AbstractProcessor {
@ -29,8 +31,10 @@ public class WaitingTasksProcessor extends AbstractProcessor {
for (WaitingProcessor p : waitingTasks) { for (WaitingProcessor p : waitingTasks) {
QueueTask wTask = QueueTask wTask =
new QueueTask(task.getUser(), task.getTitle() + " / " + p.getTitle(), task.getTitle() + " / " + p.getTitle(), p.getProc()); new QueueTask(task.getUser(), task.getTitle() + " / " + p.getTitle(), task.getTitle() + " / " + p.getTitle(), p.getProc());
transaction.getEntityManager().persist(wTask); final EntityManager entityManager = transaction.getEntityManager();
transaction.enqueue(p.getHost(), wTask); entityManager.persist(wTask);
entityManager.refresh(wTask);
transaction.enqueue(p.getHost(), TaskTransfer.getTransferObject(wTask));
} }
} }
super.finalize(transaction, task); super.finalize(transaction, task);

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.hsadmin.core</groupId> <groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId> <artifactId>hsadmin-util</artifactId>
<version>4.0.15</version> <version>4.0.16</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version> <java.version>11</java.version>