null handling

This commit is contained in:
Peter Hormanns 2023-09-05 12:12:13 +02:00
parent 6e39b2e883
commit 35862c1c06
11 changed files with 254 additions and 2 deletions

View File

@ -112,6 +112,7 @@ public class QueueStatusReceiverServlet extends HttpServlet
@Override @Override
public void onMessage(Message jmsMessage) { public void onMessage(Message jmsMessage) {
assert jmsMessage != null;
Transaction transaction = null; Transaction transaction = null;
messageCount++; messageCount++;
try { try {
@ -123,7 +124,13 @@ public class QueueStatusReceiverServlet extends HttpServlet
transaction.beginTransaction(); transaction.beginTransaction();
EntityManager em = transaction.getEntityManager(); EntityManager em = transaction.getEntityManager();
em.clear(); em.clear();
QueueTask persistentQT = em.find(QueueTask.class, detachedQT.getId()); final long queueTaskId = detachedQT.getId();
QueueTask persistentQT = em.find(QueueTask.class, queueTaskId);
if (persistentQT == null) {
jmsMessage.acknowledge();
errorCount++;
throw new TechnicalException("QueueTask not found, id: " + queueTaskId);
}
persistentQT.assign(detachedQT); persistentQT.assign(detachedQT);
Processor processor = persistentQT.getProcessor(); Processor processor = persistentQT.getProcessor();
if (processor != null) { if (processor != null) {
@ -133,6 +140,7 @@ public class QueueStatusReceiverServlet extends HttpServlet
em.flush(); em.flush();
transaction.commitTransaction(); transaction.commitTransaction();
} else { } else {
errorCount++;
final String err = "Deserialization failed " + new Date(); final String err = "Deserialization failed " + new Date();
System.out.println(err); System.out.println(err);
throw new TechnicalException(err); throw new TechnicalException(err);

27
qserv-test/.classpath Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

1
qserv-test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target/

23
qserv-test/.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hsadmin-qserv-test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,3 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

68
qserv-test/pom.xml Normal file
View File

@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv-test</artifactId>
<version>4.0.16</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId>
<version>4.0.16</version>
</dependency>
<dependency>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv</artifactId>
<version>4.0.16</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.17.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,71 @@
package de.hsadmin.qserv.test;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import org.apache.activemq.ActiveMQConnectionFactory;
import de.hsadmin.core.qserv.ProcessorException;
import de.hsadmin.core.qserv.TaskTransfer;
public class Pull implements MessageListener {
public static void main(String[] args) {
ActiveMQConnectionFactory cxFact = new ActiveMQConnectionFactory("ssl://hsh02.hostsharing.net:61619");
try {
// QueueConnection qCx = cxFact.createQueueConnection("hsar", "");
QueueConnection queueConnect = cxFact.createQueueConnection("user-h98", "");
QueueSession session = queueConnect.createQueueSession(false, QueueSession.CLIENT_ACKNOWLEDGE);
Queue queue = session.createQueue("queue.hsadminSystem-h98");
QueueReceiver receiver = session.createReceiver(queue);
receiver.setMessageListener(new Pull());
queueConnect.start();
boolean doContinue = true;
while (doContinue) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
doContinue = false;
}
}
queueConnect.stop();
session.close();
queueConnect.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onMessage(Message message) {
if (message instanceof ObjectMessage) {
try {
ObjectMessage objMesg = (ObjectMessage) message;
Serializable serializable = objMesg.getObject();
if (serializable instanceof TaskTransfer) {
TaskTransfer task = (TaskTransfer) serializable;
task.getProcessor().process();
}
} catch (ProcessorException | JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
message.acknowledge();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,37 @@
package de.hsadmin.qserv.test;
import java.util.Date;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import org.apache.activemq.ActiveMQConnectionFactory;
import de.hsadmin.core.qserv.NullProcessor;
import de.hsadmin.core.qserv.TaskTransfer;
public class Push {
public static void main(String[] args) {
ActiveMQConnectionFactory cxFact = new ActiveMQConnectionFactory("ssl://hsh02.hostsharing.net:61619");
try {
// QueueConnection qCx = cxFact.createQueueConnection("user-h98", "");
QueueConnection qCx = cxFact.createQueueConnection("hsar", "Eix4Baijee2ae1aighetaik8");
QueueSession session = qCx.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("queue.hsadminSystem-h98");
MessageProducer producer = session.createProducer(queue);
Message message = session.createObjectMessage(new TaskTransfer(1L, "anonymous", new Date(), null, "hello world", "hello world detail", new NullProcessor(), null));
producer.send(queue, message);
session.close();
qCx.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -145,7 +145,9 @@ public class Transaction {
if (transactionActive) { if (transactionActive) {
rollbackTransaction(); rollbackTransaction();
} }
entityManager.close(); if (entityManager != null) {
entityManager.close();
}
} }
/** /**