From 6444267b8d152b4091b3a95e7bf05c79fce19b3e Mon Sep 17 00:00:00 2001 From: cmlavila Date: Tue, 10 Jul 2018 14:31:11 -0300 Subject: [PATCH] PR for last fixes after editors review (#4678) * Code for BAEL-1811 * BAEL-1811 - Fixes after editor review * Last fixes after editor review --- ejb/wildfly/wildfly-mdb/pom.xml | 21 ++++++++ .../baeldung/wildfly/mdb/ReadMessageMDB.java | 28 ++++++++++ .../wildfly/mdb/SendMessageServlet.java | 52 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 ejb/wildfly/wildfly-mdb/pom.xml create mode 100644 ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/ReadMessageMDB.java create mode 100644 ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/SendMessageServlet.java diff --git a/ejb/wildfly/wildfly-mdb/pom.xml b/ejb/wildfly/wildfly-mdb/pom.xml new file mode 100644 index 0000000000..0b2ec7d5a3 --- /dev/null +++ b/ejb/wildfly/wildfly-mdb/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + widlfly-mdb + + + com.baeldung.wildfly + wildfly-example + 0.0.1-SNAPSHOT + + + + + + javax + javaee-api + ${javaee-api.version} + provided + + + \ No newline at end of file diff --git a/ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/ReadMessageMDB.java b/ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/ReadMessageMDB.java new file mode 100644 index 0000000000..6997f92201 --- /dev/null +++ b/ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/ReadMessageMDB.java @@ -0,0 +1,28 @@ +package com.baeldung.wildfly.mdb; + +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.TextMessage; + +/** + * Message-Driven Bean implementation class for: ReadMessageMDB + */ +@MessageDriven( + activationConfig = { + @ActivationConfigProperty(propertyName = "destination", propertyValue = "tutorialQueue"), + @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") +}) +public class ReadMessageMDB implements MessageListener { + + public void onMessage(Message message) { + TextMessage textMessage = (TextMessage) message; + try { + System.out.println("Message received: " + textMessage.getText()); + } catch (JMSException e) { + System.out.println("Error while trying to consume messages: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/SendMessageServlet.java b/ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/SendMessageServlet.java new file mode 100644 index 0000000000..7dcd04b43f --- /dev/null +++ b/ejb/wildfly/wildfly-mdb/src/com/baeldung/wildfly/mdb/SendMessageServlet.java @@ -0,0 +1,52 @@ +package baeldung.com.example.servlet; + +import java.io.IOException; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +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; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +@WebServlet("/SendMessageServlet") +public class SendMessageServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { + String text = req.getParameter("text") != null ? req.getParameter("text") : "Hello World"; + + try ( + Context ic = new InitialContext(); + + ConnectionFactory cf = (ConnectionFactory) ic.lookup("/ConnectionFactory"); + Queue queue = (Queue) ic.lookup("queue/tutorialQueue"); + + Connection connection = cf.createConnection(); + ) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer publisher = session.createProducer(queue); + + connection.start(); + + TextMessage message = session.createTextMessage(text); + publisher.send(message); + + } catch (NamingException | JMSException e) { + res.getWriter().println("Error while trying to send <" + text + "> message: " + e.getMessage()); + } + + res.getWriter().println("Message sent: " + text); + } + +} \ No newline at end of file