JMSConnectorBean.java
/*
Developed with the contribution of the European Commission - Directorate General for Maritime Affairs and Fisheries
© European Union, 2015-2016.
This file is part of the Integrated Fisheries Data Management (IFDM) Suite. The IFDM Suite is free software: you can
redistribute it and/or modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or any later version. The IFDM Suite is distributed in
the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a
copy of the GNU General Public License along with the IFDM Suite. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.europa.ec.fisheries.uvms.mobileterminal.message.producer.bean;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.slf4j.LoggerFactory;
import eu.europa.ec.fisheries.uvms.mobileterminal.message.constants.MessageConstants;
@Stateless
public class JMSConnectorBean {
final static org.slf4j.Logger LOG = LoggerFactory.getLogger(JMSConnectorBean.class);
private ConnectionFactory connectionFactory;
private Connection connection;
@PostConstruct
private void connectToQueue() {
LOG.debug("Open connection to JMS broker");
InitialContext ctx;
try {
ctx = new InitialContext();
} catch (Exception e) {
LOG.error("Failed to get InitialContext",e);
throw new RuntimeException(e);
}
try {
connectionFactory = (QueueConnectionFactory) ctx.lookup(MessageConstants.CONNECTION_FACTORY);
} catch (NamingException ne) {
//if we did not find the connection factory we might need to add java:/ at the start
LOG.debug("Connection Factory lookup failed for " + MessageConstants.CONNECTION_FACTORY);
String wfName = "java:/" + MessageConstants.CONNECTION_FACTORY;
try {
LOG.debug("trying " + wfName);
connectionFactory = (QueueConnectionFactory) ctx.lookup(wfName);
} catch (Exception e) {
LOG.error("Connection Factory lookup failed for both " + MessageConstants.CONNECTION_FACTORY + " and " + wfName);
throw new RuntimeException(e);
}
}
try {
connection = connectionFactory.createConnection();
connection.start();
} catch (JMSException ex) {
LOG.error("Error when open connection to JMS broker");
}
}
public Session getNewSession() throws JMSException {
if (connection == null) {
connectToQueue();
}
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
return session;
}
public TextMessage createTextMessage(Session session, String message) throws JMSException {
return session.createTextMessage(message);
}
}