commit 03e403d7175716e363e8b0d7492501fba93ff639 Author: Wolfgang Hottgenroth Date: Wed Nov 22 17:28:57 2017 +0100 initial diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..0a1dadd --- /dev/null +++ b/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/.project b/.project new file mode 100644 index 0000000..ebfb25b --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + HottisLibJava + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..60ec346 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1e49e33 --- /dev/null +++ b/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + de.hottis.common + HottisLibJava + jar + 1.0-SNAPSHOT + HottisLibJava + http://maven.apache.org + + 1.8 + 1.8 + + + + junit + junit + 3.8.1 + test + + + org.apache.activemq + activemq-client + 5.15.2 + + + org.apache.logging.log4j + log4j-core + 2.9.1 + + + org.apache.logging.log4j + log4j-api + 2.9.1 + + + diff --git a/src/main/java/de/hottis/common/HottisCommonException.java b/src/main/java/de/hottis/common/HottisCommonException.java new file mode 100644 index 0000000..df2bfd0 --- /dev/null +++ b/src/main/java/de/hottis/common/HottisCommonException.java @@ -0,0 +1,14 @@ +package de.hottis.common; + +public class HottisCommonException extends Exception { + private static final long serialVersionUID = 1L; + + public HottisCommonException(String msg, Throwable cause) { + super(msg, cause); + } + + public HottisCommonException(String msg) { + super(msg); + } + +} diff --git a/src/main/java/de/hottis/common/ITriggerable.java b/src/main/java/de/hottis/common/ITriggerable.java new file mode 100644 index 0000000..7f6d0d9 --- /dev/null +++ b/src/main/java/de/hottis/common/ITriggerable.java @@ -0,0 +1,5 @@ +package de.hottis.common; + +public interface ITriggerable { + public void trigger(); +} diff --git a/src/main/java/de/hottis/common/JmsTopic.java b/src/main/java/de/hottis/common/JmsTopic.java new file mode 100644 index 0000000..a6fc625 --- /dev/null +++ b/src/main/java/de/hottis/common/JmsTopic.java @@ -0,0 +1,124 @@ +package de.hottis.common; + +import java.io.Serializable; +import java.util.Properties; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import javax.jms.Topic; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class JmsTopic implements MyQueue { + static final String JMS_PARSED_DATA_TOPIC = "jms.parseddata.topic"; + static final String JMS_CLIENTID_PROP = "jms.clientid"; + static final String JMS_BROKER ="jms.broker"; + + static public enum Mode { CONSUMER, PRODUCER, CONSUMER_PRODUCER }; + + final protected Logger logger = LogManager.getRootLogger(); + + private String clientId; + private Properties config; + private Connection connection; + private Session session; + private Topic topic; + private MessageProducer producer; + private MessageConsumer consumer; + private String topicTxt; + private String broker; + private Mode mode; + + public JmsTopic(Properties config, Mode mode) { + this.config = config; + clientId = this.config.getProperty(JMS_CLIENTID_PROP); + topicTxt = this.config.getProperty(JMS_PARSED_DATA_TOPIC); + broker = this.config.getProperty(JMS_BROKER); + this.mode = mode; + } + + + public void init() throws HottisCommonException { + try { + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(broker); + connectionFactory.setTrustAllPackages(true); + logger.debug("connectionFactory: " + connectionFactory); + connection = connectionFactory.createConnection(); + connection.setClientID(clientId); + connection.start(); + logger.debug("connection: " + connection); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + logger.debug("session: " + session); + topic = session.createTopic(topicTxt); + logger.debug("topic: " + topic); + + if ((mode == Mode.PRODUCER) || (mode == Mode.CONSUMER_PRODUCER)) { + producer = session.createProducer(topic); + producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + logger.debug("producer: " + producer); + } else { + producer = null; + } + + if ((mode == Mode.CONSUMER) || (mode == Mode.CONSUMER_PRODUCER)) { + consumer = session.createDurableSubscriber(topic, "DatabaseEngine"); + logger.debug("consumer: " + consumer); + } else { + consumer = null; + } + + } catch (JMSException e) { + logger.error("JMSException in AJmsQueue.init", e); + throw new HottisCommonException("JMSException in AJmsTopic.init", e); + } + } + + public T dequeue() throws HottisCommonException { + if (consumer == null) { + throw new HottisCommonException("This is no consumer"); + } + try { + ObjectMessage message = (ObjectMessage) consumer.receiveNoWait(); + T item; + if (message != null) { + @SuppressWarnings("unchecked") + T t = (T) message.getObject(); + item = t; + logger.debug("message dequeued"); + } else { + item = null; + } + return item; + } catch (JMSException e) { + logger.error("JMSException in JmsTopic.dequeue", e); + logger.error("Calling init"); + init(); + throw new HottisCommonException("JMSException in JmsTopic.dequeue", e); + } + } + + public void enqueue(T item) throws HottisCommonException { + if (producer == null) { + throw new HottisCommonException("This is no producer"); + } + try { + ObjectMessage message = session.createObjectMessage(); + message.setObject(item); + producer.send(message); + logger.debug("message enqueued"); + } catch (JMSException e) { + logger.error("JMSException in JmsTopic.enqueue", e); + logger.error("Calling init"); + init(); + throw new HottisCommonException("JMSException in JmsTopic.enqueue", e); + } + } + +} diff --git a/src/main/java/de/hottis/common/MyQueue.java b/src/main/java/de/hottis/common/MyQueue.java new file mode 100644 index 0000000..8b81acb --- /dev/null +++ b/src/main/java/de/hottis/common/MyQueue.java @@ -0,0 +1,8 @@ +package de.hottis.common; + +import java.io.Serializable; + +public interface MyQueue { + public T dequeue() throws HottisCommonException; + public void enqueue(T item) throws HottisCommonException; +} diff --git a/src/main/java/de/hottis/common/TriggerTimer.java b/src/main/java/de/hottis/common/TriggerTimer.java new file mode 100644 index 0000000..624f195 --- /dev/null +++ b/src/main/java/de/hottis/common/TriggerTimer.java @@ -0,0 +1,15 @@ +package de.hottis.common; + +import java.util.TimerTask; + +public class TriggerTimer extends TimerTask { + private ITriggerable triggerable; + + public TriggerTimer(ITriggerable triggerable) { + this.triggerable = triggerable; + } + + public void run() { + triggerable.trigger(); + } +} diff --git a/src/test/java/de/hottis/common/AppTest.java b/src/test/java/de/hottis/common/AppTest.java new file mode 100644 index 0000000..38463cc --- /dev/null +++ b/src/test/java/de/hottis/common/AppTest.java @@ -0,0 +1,38 @@ +package de.hottis.common; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}