PR for last fixes after editors review (#4678)
* Code for BAEL-1811 * BAEL-1811 - Fixes after editor review * Last fixes after editor review
This commit is contained in:
parent
30fef21549
commit
6444267b8d
|
@ -0,0 +1,21 @@
|
|||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>widlfly-mdb</artifactId>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.wildfly</groupId>
|
||||
<artifactId>wildfly-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Dependency for java ee -->
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>${javaee-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue