commit 9bc0e44ef641120f21661f3450d29893d28194a5 Author: 2wenty1ne Date: Wed Nov 5 18:02:16 2025 +0100 Finished Reverser diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/VAR-JMS/.classpath b/VAR-JMS/.classpath new file mode 100644 index 0000000..b8a9f21 --- /dev/null +++ b/VAR-JMS/.classpath @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/VAR-JMS/.project b/VAR-JMS/.project new file mode 100644 index 0000000..ef2495b --- /dev/null +++ b/VAR-JMS/.project @@ -0,0 +1,17 @@ + + + VAR-JMS + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/VAR-JMS/.settings/org.eclipse.jdt.core.prefs b/VAR-JMS/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..e40c773 --- /dev/null +++ b/VAR-JMS/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.release=disabled +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/VAR-JMS/VAR-JMS.iml b/VAR-JMS/VAR-JMS.iml new file mode 100644 index 0000000..f09bb38 --- /dev/null +++ b/VAR-JMS/VAR-JMS.iml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VAR-JMS/bin/jndi.properties b/VAR-JMS/bin/jndi.properties new file mode 100644 index 0000000..ec01346 --- /dev/null +++ b/VAR-JMS/bin/jndi.properties @@ -0,0 +1,18 @@ +java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory +java.naming.provider.url = tcp://localhost:61616 + +## var.mom.jms.client +queue.var.mom.jms.client.queue1 = queue4731 +queue.var.mom.jms.client.queue2 = queue4732 + +## var.mom.jms.log +queue.var.mom.jms.log.queue = queue4733 + +## var.mom.jms.log +queue.var.mom.jms.echo.queue = queue4734 + +## var.mom.jms.chat +topic.var.mom.jms.channel1 = topic4735 + +## var.mom.jms.reverse +queue.var.mom.jms.reverse.queue = queue4736 \ No newline at end of file diff --git a/VAR-JMS/bin/var/mom/jms/chat/ChatClient.class b/VAR-JMS/bin/var/mom/jms/chat/ChatClient.class new file mode 100644 index 0000000..6a5f6af Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/chat/ChatClient.class differ diff --git a/VAR-JMS/bin/var/mom/jms/client/JMSClient.class b/VAR-JMS/bin/var/mom/jms/client/JMSClient.class new file mode 100644 index 0000000..ac3065c Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/client/JMSClient.class differ diff --git a/VAR-JMS/bin/var/mom/jms/echo/Conf.class b/VAR-JMS/bin/var/mom/jms/echo/Conf.class new file mode 100644 index 0000000..4462a7f Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/echo/Conf.class differ diff --git a/VAR-JMS/bin/var/mom/jms/echo/EchoReplierNode.class b/VAR-JMS/bin/var/mom/jms/echo/EchoReplierNode.class new file mode 100644 index 0000000..b3406da Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/echo/EchoReplierNode.class differ diff --git a/VAR-JMS/bin/var/mom/jms/echo/EchoRequesterNode.class b/VAR-JMS/bin/var/mom/jms/echo/EchoRequesterNode.class new file mode 100644 index 0000000..ecec6fc Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/echo/EchoRequesterNode.class differ diff --git a/VAR-JMS/bin/var/mom/jms/log/Conf.class b/VAR-JMS/bin/var/mom/jms/log/Conf.class new file mode 100644 index 0000000..8095196 Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/log/Conf.class differ diff --git a/VAR-JMS/bin/var/mom/jms/log/ConsumerCallbackNode.class b/VAR-JMS/bin/var/mom/jms/log/ConsumerCallbackNode.class new file mode 100644 index 0000000..950a841 Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/log/ConsumerCallbackNode.class differ diff --git a/VAR-JMS/bin/var/mom/jms/log/ConsumerFilteredNode.class b/VAR-JMS/bin/var/mom/jms/log/ConsumerFilteredNode.class new file mode 100644 index 0000000..a83f800 Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/log/ConsumerFilteredNode.class differ diff --git a/VAR-JMS/bin/var/mom/jms/log/ConsumerPullNode.class b/VAR-JMS/bin/var/mom/jms/log/ConsumerPullNode.class new file mode 100644 index 0000000..75bbb6f Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/log/ConsumerPullNode.class differ diff --git a/VAR-JMS/bin/var/mom/jms/log/ProducerNode.class b/VAR-JMS/bin/var/mom/jms/log/ProducerNode.class new file mode 100644 index 0000000..cefe534 Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/log/ProducerNode.class differ diff --git a/VAR-JMS/bin/var/mom/jms/reverse/Conf.class b/VAR-JMS/bin/var/mom/jms/reverse/Conf.class new file mode 100644 index 0000000..ef99ce5 Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/reverse/Conf.class differ diff --git a/VAR-JMS/bin/var/mom/jms/reverse/Requester.class b/VAR-JMS/bin/var/mom/jms/reverse/Requester.class new file mode 100644 index 0000000..943063a Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/reverse/Requester.class differ diff --git a/VAR-JMS/bin/var/mom/jms/reverse/Reverser.class b/VAR-JMS/bin/var/mom/jms/reverse/Reverser.class new file mode 100644 index 0000000..c2d5f92 Binary files /dev/null and b/VAR-JMS/bin/var/mom/jms/reverse/Reverser.class differ diff --git a/VAR-JMS/lib/activemq-all-5.14.1.jar b/VAR-JMS/lib/activemq-all-5.14.1.jar new file mode 100644 index 0000000..25e74cd Binary files /dev/null and b/VAR-JMS/lib/activemq-all-5.14.1.jar differ diff --git a/VAR-JMS/lib/javax.activation-api-1.2.0.jar b/VAR-JMS/lib/javax.activation-api-1.2.0.jar new file mode 100644 index 0000000..986c365 Binary files /dev/null and b/VAR-JMS/lib/javax.activation-api-1.2.0.jar differ diff --git a/VAR-JMS/lib/jaxb-api-2.3.1.jar b/VAR-JMS/lib/jaxb-api-2.3.1.jar new file mode 100644 index 0000000..4565865 Binary files /dev/null and b/VAR-JMS/lib/jaxb-api-2.3.1.jar differ diff --git a/VAR-JMS/src/jndi.properties b/VAR-JMS/src/jndi.properties new file mode 100644 index 0000000..ec01346 --- /dev/null +++ b/VAR-JMS/src/jndi.properties @@ -0,0 +1,18 @@ +java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory +java.naming.provider.url = tcp://localhost:61616 + +## var.mom.jms.client +queue.var.mom.jms.client.queue1 = queue4731 +queue.var.mom.jms.client.queue2 = queue4732 + +## var.mom.jms.log +queue.var.mom.jms.log.queue = queue4733 + +## var.mom.jms.log +queue.var.mom.jms.echo.queue = queue4734 + +## var.mom.jms.chat +topic.var.mom.jms.channel1 = topic4735 + +## var.mom.jms.reverse +queue.var.mom.jms.reverse.queue = queue4736 \ No newline at end of file diff --git a/VAR-JMS/src/var/mom/jms/chat/ChatClient.java b/VAR-JMS/src/var/mom/jms/chat/ChatClient.java new file mode 100644 index 0000000..24169f3 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/chat/ChatClient.java @@ -0,0 +1,69 @@ +package var.mom.jms.chat; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +// Code aus JMS-Client mit Umbenennung des Typs von JMSClient zu ChatClient und Anpassung des packages +public class ChatClient implements MessageListener { + private Connection connection; + private Session session; + private MessageConsumer consumer; + + public ChatClient() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + Destination queue = (Destination) ctx.lookup("var.mom.jms.chat.queue"); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumer = session.createConsumer(queue); + consumer.setMessageListener(this); + connection.start(); + } + + @Override + public void onMessage(Message message) { + try { + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + String messageText = textMessage.getText(); + String priority = textMessage.getStringProperty("Priority"); + System.out.println(messageText + " [Priority=" + priority + "]"); + } + } catch (JMSException e) { + System.err.println(e); + } + + } + + public static void main(String[] args) { + long wait = Long.parseLong(args[0]); + ChatClient node = null; + try { + node = new ChatClient(); + Thread.sleep(wait); + } catch (InterruptedException | NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.consumer != null) + node.consumer.close(); + if (node != null && node.session != null) + node.session.close(); + if (node != null && node.connection != null) + node.connection.close(); + } catch (JMSException e) { + System.err.println(e); + } + } + } +} diff --git a/VAR-JMS/src/var/mom/jms/client/JMSClient.java b/VAR-JMS/src/var/mom/jms/client/JMSClient.java new file mode 100644 index 0000000..3df9bb9 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/client/JMSClient.java @@ -0,0 +1,84 @@ +package var.mom.jms.client; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import javax.jms.BytesMessage; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class JMSClient implements MessageListener { + private Connection connection; + private Session session; + private MessageProducer producer; + private MessageConsumer consumer; + + public JMSClient(String sendDest, String receiveDest) throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination destOut = (Destination) ctx.lookup(sendDest); + Destination destIn = (Destination) ctx.lookup(receiveDest); + producer = session.createProducer(destOut); + consumer = session.createConsumer(destIn); + consumer.setMessageListener(this); + connection.start(); + } + + @Override + public void onMessage(Message message) { + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + try { + System.out.println(textMessage.getText()); + } catch (JMSException e) { + System.err.println(e); + } + } + } + + public static void main(String[] args) { + JMSClient node = null; + try { + node = new JMSClient("var.mom.jms.client.queue1", "var.mom.jms.client.queue2"); + BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + String line; + while (true) { + line = input.readLine(); + node.producer.send(node.session.createTextMessage(line)); + } + } catch (NamingException | JMSException | IOException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.producer != null) { + node.producer.close(); + } + if (node != null && node.consumer != null) { + node.consumer.close(); + } + if (node != null && node.session != null) { + node.session.close(); + } + if (node != null && node.connection != null) { + node.connection.close(); + } + } catch (JMSException e) { + System.err.println(e); + } + } + } +} \ No newline at end of file diff --git a/VAR-JMS/src/var/mom/jms/echo/Conf.java b/VAR-JMS/src/var/mom/jms/echo/Conf.java new file mode 100644 index 0000000..719fbaa --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/echo/Conf.java @@ -0,0 +1,5 @@ +package var.mom.jms.echo; + +public class Conf { + public static final String QUEUE = "var.mom.jms.echo.queue"; +} diff --git a/VAR-JMS/src/var/mom/jms/echo/EchoReplierNode.java b/VAR-JMS/src/var/mom/jms/echo/EchoReplierNode.java new file mode 100644 index 0000000..66483d1 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/echo/EchoReplierNode.java @@ -0,0 +1,74 @@ +package var.mom.jms.echo; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class EchoReplierNode implements MessageListener { + private Connection connection; + private Session session; + private MessageConsumer consumer; + + public EchoReplierNode() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + Queue queue = (Queue) ctx.lookup(Conf.QUEUE); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumer = session.createConsumer(queue); + consumer.setMessageListener(this); + connection.start(); + } + + @Override + public void onMessage(Message request) { + try { + if (request instanceof TextMessage) { + TextMessage requestText = (TextMessage) request; + System.out.println("empfangen: " + requestText.getText()); + MessageProducer replyProducer = session.createProducer(request.getJMSReplyTo()); + TextMessage reply = session.createTextMessage(); + reply.setText("echo: " + requestText.getText()); + Thread.sleep(5000); + replyProducer.send(reply); + replyProducer.close(); + } + } catch (JMSException | InterruptedException e) { + System.err.println(e); + } + + } + + public static void main(String[] args) { + EchoReplierNode node = null; + try { + node = new EchoReplierNode(); + while (true) { + Thread.sleep(1000); + } + } catch (InterruptedException | NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.consumer != null) + node.consumer.close(); + if (node != null && node.session != null) + node.session.close(); + if (node != null && node.connection != null) + node.connection.close(); + } catch (JMSException e) { + System.err.println(e); + } + } + } +} diff --git a/VAR-JMS/src/var/mom/jms/echo/EchoRequesterNode.java b/VAR-JMS/src/var/mom/jms/echo/EchoRequesterNode.java new file mode 100644 index 0000000..2457ff1 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/echo/EchoRequesterNode.java @@ -0,0 +1,85 @@ +package var.mom.jms.echo; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class EchoRequesterNode { + private Connection connection; + private Session session; + private MessageProducer producer; + private MessageConsumer consumer; + private Queue replyQueue; + + public EchoRequesterNode() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Queue queue = (Queue) ctx.lookup(Conf.QUEUE); + producer = session.createProducer(queue); + replyQueue = session.createTemporaryQueue(); + consumer = session.createConsumer(replyQueue); + connection.start(); + } + + public void receiveAndPrintMessages() throws JMSException { + Message request; + while ((request = consumer.receive()) != null) { + try { + if (request instanceof TextMessage) { + TextMessage requestText = (TextMessage) request; + String messageText = requestText.getText(); + System.out.println("empfangen: " + messageText); + } + } catch (JMSException e) { + System.err.println(e); + } + } + } + + public void sendMessage(String text) throws JMSException { + TextMessage message = session.createTextMessage(); + message.setText(text); + message.setJMSReplyTo(replyQueue); + producer.send(message); + } + + public static void main(String[] args) { + String text = args[0]; + EchoRequesterNode node = null; + try { + node = new EchoRequesterNode(); + node.sendMessage(text); + node.receiveAndPrintMessages(); + } catch (NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.producer != null) { + node.producer.close(); + } + if (node != null && node.consumer != null) { + node.consumer.close(); + } + if (node != null && node.session != null) { + node.session.close(); + } + if (node != null && node.connection != null) { + node.connection.close(); + } + } catch (JMSException e) { + System.err.println(e); + } + } + } +} diff --git a/VAR-JMS/src/var/mom/jms/log/Conf.java b/VAR-JMS/src/var/mom/jms/log/Conf.java new file mode 100644 index 0000000..fcd74c0 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/log/Conf.java @@ -0,0 +1,5 @@ +package var.mom.jms.log; + +public class Conf { + public static final String QUEUE = "var.mom.jms.log.queue"; +} diff --git a/VAR-JMS/src/var/mom/jms/log/ConsumerCallbackNode.java b/VAR-JMS/src/var/mom/jms/log/ConsumerCallbackNode.java new file mode 100644 index 0000000..e498d2a --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/log/ConsumerCallbackNode.java @@ -0,0 +1,68 @@ +package var.mom.jms.log; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class ConsumerCallbackNode implements MessageListener { + private Connection connection; + private Session session; + private MessageConsumer consumer; + + public ConsumerCallbackNode() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + Destination queue = (Destination) ctx.lookup(Conf.QUEUE); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumer = session.createConsumer(queue); + consumer.setMessageListener(this); + connection.start(); + } + + @Override + public void onMessage(Message message) { + try { + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + String messageText = textMessage.getText(); + String priority = textMessage.getStringProperty("Priority"); + System.out.println(messageText + " [Priority=" + priority + "]"); + } + } catch (JMSException e) { + System.err.println(e); + } + + } + + public static void main(String[] args) { + long wait = Long.parseLong(args[0]); + ConsumerCallbackNode node = null; + try { + node = new ConsumerCallbackNode(); + Thread.sleep(wait); + } catch (InterruptedException | NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.consumer != null) + node.consumer.close(); + if (node != null && node.session != null) + node.session.close(); + if (node != null && node.connection != null) + node.connection.close(); + } catch (JMSException e) { + System.err.println(e); + } + } + } +} diff --git a/VAR-JMS/src/var/mom/jms/log/ConsumerFilteredNode.java b/VAR-JMS/src/var/mom/jms/log/ConsumerFilteredNode.java new file mode 100644 index 0000000..b4a851d --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/log/ConsumerFilteredNode.java @@ -0,0 +1,68 @@ +package var.mom.jms.log; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class ConsumerFilteredNode implements MessageListener { + private Connection connection; + private Session session; + private MessageConsumer consumer; + + public ConsumerFilteredNode() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + Destination queue = (Destination) ctx.lookup(Conf.QUEUE); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumer = session.createConsumer(queue, "Priority='high'"); + consumer.setMessageListener(this); + connection.start(); + } + + @Override + public void onMessage(Message message) { + try { + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + String messageText = textMessage.getText(); + String priority = textMessage.getStringProperty("Priority"); + System.out.println(messageText + " [Priority=" + priority + "]"); + } + } catch (JMSException e) { + System.err.println(e); + } + } + + public static void main(String[] args) { + //long wait = Long.parseLong(args[0]); + ConsumerFilteredNode node = null; + try { + node = new ConsumerFilteredNode(); + Thread.sleep(10000000); + } catch (InterruptedException | NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.consumer != null) + node.consumer.close(); + if (node != null && node.session != null) + node.session.close(); + if (node != null && node.connection != null) + node.connection.close(); + } catch (JMSException e) { + System.err.println(e); + } + } + } + +} diff --git a/VAR-JMS/src/var/mom/jms/log/ConsumerPullNode.java b/VAR-JMS/src/var/mom/jms/log/ConsumerPullNode.java new file mode 100644 index 0000000..affa2e9 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/log/ConsumerPullNode.java @@ -0,0 +1,62 @@ +package var.mom.jms.log; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class ConsumerPullNode { + private Connection connection; + private Session session; + private MessageConsumer consumer; + + public ConsumerPullNode() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + Destination queue = (Destination) ctx.lookup(Conf.QUEUE); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + consumer = session.createConsumer(queue); + connection.start(); + } + + public void receiveAndPrintMessages(long timeout) throws JMSException { + Message message; + while ((message = consumer.receive(timeout)) != null) { + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + String messageText = textMessage.getText(); + System.out.println(messageText); + } + } + } + + public static void main(String[] args) { + //long timeout = Long.parseLong(args[0]); + ConsumerPullNode node = null; + try { + node = new ConsumerPullNode(); + node.receiveAndPrintMessages(0); + } catch (NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.consumer != null) + node.consumer.close(); + if (node != null && node.session != null) + node.session.close(); + if (node != null && node.connection != null) + node.connection.close(); + } catch (JMSException e) { + System.err.println(e); + } + } + } +} \ No newline at end of file diff --git a/VAR-JMS/src/var/mom/jms/log/ProducerNode.java b/VAR-JMS/src/var/mom/jms/log/ProducerNode.java new file mode 100644 index 0000000..eee04d2 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/log/ProducerNode.java @@ -0,0 +1,60 @@ +package var.mom.jms.log; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class ProducerNode { + private Connection connection; + private Session session; + private MessageProducer producer; + + public ProducerNode() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination queue = (Destination) ctx.lookup(Conf.QUEUE); + producer = session.createProducer(queue); + } + + public void sendMessage(String text, String priority) throws JMSException { + TextMessage message = session.createTextMessage(); + message.setText(text); + message.setStringProperty("Priority", priority); + producer.send(message); + } + + public static void main(String[] args) { + String text = "Hi :)"; + String priority = "high"; + ProducerNode node = null; + try { + node = new ProducerNode(); + node.sendMessage(text, priority); + } catch (NamingException | JMSException e) { + System.err.println(e); + } finally { + try { + if (node != null && node.producer != null) { + node.producer.close(); + } + if (node != null && node.session != null) { + node.session.close(); + } + if (node != null && node.connection != null) { + node.connection.close(); + } + } catch (JMSException e) { + System.err.println(e); + } + } + } +} diff --git a/VAR-JMS/src/var/mom/jms/reverse/Conf.java b/VAR-JMS/src/var/mom/jms/reverse/Conf.java new file mode 100644 index 0000000..fe6037e --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/reverse/Conf.java @@ -0,0 +1,5 @@ +package var.mom.jms.reverse; + +public class Conf { + public static final String QUEUE = "var.mom.jms.reverse.queue"; +} diff --git a/VAR-JMS/src/var/mom/jms/reverse/Requester.java b/VAR-JMS/src/var/mom/jms/reverse/Requester.java new file mode 100644 index 0000000..fc70d24 --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/reverse/Requester.java @@ -0,0 +1,79 @@ +package var.mom.jms.reverse; + +import var.mom.jms.log.ProducerNode; + +import javax.jms.*; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class Requester { + private Connection connection; + private Session session; + private MessageProducer producer; + private Queue replyQueue; + private MessageConsumer consumer; + + public Requester() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + Destination queue = (Destination) ctx.lookup(Conf.QUEUE); + producer = session.createProducer(queue); + + replyQueue = session.createTemporaryQueue(); + consumer = session.createConsumer(replyQueue); + + connection.start(); + } + + public void sendMessage(String text, String priority) throws JMSException { + TextMessage message = session.createTextMessage(); + message.setText(text); + message.setStringProperty("Priority", priority); + + message.setJMSReplyTo(replyQueue); + producer.send(message); + } + public void receiveMessage() throws JMSException { + Message message = consumer.receive(); + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + System.out.println("Received: " + textMessage.getText()); + } + } + + static void main(String[] args) { + String text = "Hiiiiiiillll"; + String priority = "High"; + Requester node = null; + + try { + node = new Requester(); + node.sendMessage(text, priority); + node.receiveMessage(); + + } + catch (NamingException | JMSException e) { + System.err.println(e); + } + finally { + try { + if (node != null && node.producer != null) { + node.producer.close(); + } + if (node != null && node.session != null) { + node.session.close(); + } + if (node != null && node.connection != null) { + node.connection.close(); + } + } catch (JMSException e) { + System.err.println(e); + } + } + } +} diff --git a/VAR-JMS/src/var/mom/jms/reverse/Reverser.java b/VAR-JMS/src/var/mom/jms/reverse/Reverser.java new file mode 100644 index 0000000..083eb4f --- /dev/null +++ b/VAR-JMS/src/var/mom/jms/reverse/Reverser.java @@ -0,0 +1,82 @@ +package var.mom.jms.reverse; + +import org.w3c.dom.Text; + +import javax.jms.*; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class Reverser implements MessageListener { + private Connection connection; + private Session session; + private MessageConsumer consumer; + + public Reverser() throws NamingException, JMSException { + Context ctx = new InitialContext(); + ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); + + connection = factory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + Destination queue = (Destination) ctx.lookup(Conf.QUEUE); + consumer = session.createConsumer(queue); + consumer.setMessageListener(this); + + connection.start(); + } + + @Override + public void onMessage(Message message) { + try { + if (message instanceof TextMessage) { + TextMessage textMessage = (TextMessage) message; + + System.out.println("Received: " + textMessage.getText()); + + StringBuilder sb = new StringBuilder(); + sb.append(textMessage.getText()); + sb.reverse(); + + Destination replyQueue = message.getJMSReplyTo(); + MessageProducer producer = session.createProducer(replyQueue); + + TextMessage replyMessage = session.createTextMessage(); + replyMessage.setText(sb.toString()); + replyMessage.setStringProperty("Priority", message.getStringProperty("Priority")); + + producer.send(replyMessage); + } + } catch (JMSException e) { + System.err.println(e); + } + } + + static void main(String[] args) { + Reverser node = null; + + try { + node = new Reverser(); + Thread.sleep(1000000); + } + catch (NamingException | JMSException | InterruptedException e) { + System.err.println(e); + } + finally { + try { + if (node != null && node.consumer != null) { + node.consumer.close(); + } + if (node != null && node.session != null) { + node.session.close(); + } + if (node != null && node.connection != null) { + node.connection.close(); + } + } + catch (JMSException e) { + throw new RuntimeException(e); + } + } + } +}