Skip to content

Commit

Permalink
Create Session and MessageProducer in Transmitter.init(). Fixes #85.
Browse files Browse the repository at this point in the history
  • Loading branch information
RKrahl committed May 18, 2018
1 parent 49aa174 commit 6f95ac3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/icatproject/ids/Transmitter.java
Expand Up @@ -28,6 +28,10 @@ public class Transmitter {

private TopicConnection topicConnection;

private Session jmsSession;

private MessageProducer jmsProducer;

@PostConstruct
private void init() {

Expand All @@ -38,6 +42,8 @@ private void init() {
.lookup(propertyHandler.getJmsTopicConnectionFactory());
topicConnection = topicConnectionFactory.createTopicConnection();
topic = (Topic) ic.lookup("jms/IDS/log");
jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
jmsProducer = jmsSession.createProducer(topic);
logger.info("Transmitter created");
} catch (JMSException | NamingException e) {
logger.error(fatal, "Problem with JMS " + e);
Expand All @@ -49,6 +55,9 @@ private void init() {
@PreDestroy()
private void exit() {
try {
if (jmsSession != null) {
jmsSession.close();
}
if (topicConnection != null) {
topicConnection.close();
}
Expand All @@ -60,16 +69,13 @@ private void exit() {

public void processMessage(String operation, String ip, String body, long startMillis) {
try {
Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage jmsg = jmsSession.createTextMessage(body);
jmsg.setStringProperty("operation", operation);
jmsg.setStringProperty("ip", ip);
jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis);
jmsg.setLongProperty("start", startMillis);
MessageProducer jmsProducer = jmsSession.createProducer(topic);
jmsProducer.send(jmsg);
logger.debug("Sent jms message " + operation + " " + ip);
jmsSession.close();
} catch (JMSException e) {
logger.error("Failed to send jms message " + operation + " " + ip);
}
Expand Down

0 comments on commit 6f95ac3

Please sign in to comment.