forked from JavaWebinar/masterjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c89512
commit 838ec71
Showing
8 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...l-service/src/main/java/ru/javaops/masterjava/service/mail/listeners/JmsMailListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ru.javaops.masterjava.service.mail.listeners; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.jms.*; | ||
import javax.naming.InitialContext; | ||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
import javax.servlet.annotation.WebListener; | ||
|
||
@WebListener | ||
@Slf4j | ||
public class JmsMailListener implements ServletContextListener { | ||
private Thread listenerThread = null; | ||
private QueueConnection connection; | ||
|
||
@Override | ||
public void contextInitialized(ServletContextEvent sce) { | ||
try { | ||
InitialContext initCtx = new InitialContext(); | ||
QueueConnectionFactory connectionFactory = | ||
(QueueConnectionFactory) initCtx.lookup("java:comp/env/jms/ConnectionFactory"); | ||
connection = connectionFactory.createQueueConnection(); | ||
QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | ||
Queue queue = (Queue) initCtx.lookup("java:comp/env/jms/queue/MailQueue"); | ||
QueueReceiver receiver = queueSession.createReceiver(queue); | ||
connection.start(); | ||
log.info("Listen JMS messages ..."); | ||
listenerThread = new Thread(() -> { | ||
try { | ||
while (!Thread.interrupted()) { | ||
Message m = receiver.receive(); | ||
// TODO implement mail sending | ||
if (m instanceof TextMessage) { | ||
TextMessage tm = (TextMessage) m; | ||
String text = tm.getText(); | ||
log.info("Received TextMessage with text '{}'", text); | ||
} | ||
} | ||
} catch (Exception e) { | ||
log.error("Receiving messages failed: " + e.getMessage(), e); | ||
} | ||
}); | ||
listenerThread.start(); | ||
} catch (Exception e) { | ||
log.error("JMS failed: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent sce) { | ||
if (connection != null) { | ||
try { | ||
connection.close(); | ||
} catch (JMSException ex) { | ||
log.warn("Couldn't close JMSConnection: ", ex); | ||
} | ||
} | ||
if (listenerThread != null) { | ||
listenerThread.interrupt(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
web/webapp/src/main/java/ru/javaops/masterjava/webapp/JmsSendServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ru.javaops.masterjava.webapp; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.jms.*; | ||
import javax.naming.InitialContext; | ||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.lang.IllegalStateException; | ||
|
||
@WebServlet("/sendJms") | ||
@Slf4j | ||
public class JmsSendServlet extends HttpServlet { | ||
private Connection connection; | ||
private Session session; | ||
private MessageProducer producer; | ||
|
||
@Override | ||
public void init(ServletConfig config) throws ServletException { | ||
super.init(config); | ||
try { | ||
InitialContext initCtx = new InitialContext(); | ||
ConnectionFactory connectionFactory = (ConnectionFactory) initCtx.lookup("java:comp/env/jms/ConnectionFactory"); | ||
connection = connectionFactory.createConnection(); | ||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
producer = session.createProducer((Destination) initCtx.lookup("java:comp/env/jms/queue/MailQueue")); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("JMS init failed", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
if (connection != null) { | ||
try { | ||
connection.close(); | ||
} catch (JMSException ex) { | ||
log.warn("Couldn't close JMSConnection: ", ex); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
String result; | ||
try { | ||
log.info("Start sending"); | ||
req.setCharacterEncoding("UTF-8"); | ||
resp.setCharacterEncoding("UTF-8"); | ||
String users = req.getParameter("users"); | ||
String subject = req.getParameter("subject"); | ||
String body = req.getParameter("body"); | ||
result = sendJms(users, subject, body); | ||
log.info("Processing finished with result: {}", result); | ||
} catch (Exception e) { | ||
log.error("Processing failed", e); | ||
result = e.toString(); | ||
} | ||
resp.getWriter().write(result); | ||
} | ||
|
||
private synchronized String sendJms(String users, String subject, String body) throws JMSException { | ||
TextMessage testMessage = session.createTextMessage(); | ||
testMessage.setText(subject); | ||
producer.send(testMessage); | ||
return "Successfully sent JMS message"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters