I created a standalone Java application that acts as a consumer that retrieves/listens for messages inside an ActiveMQ queue. Planning to run two instances of these on different machines for high availability purposes.What bothers me is the usage of Connection, Session, MessageConsumer, and Transaction (Hibernate). I didn't want to open a new Connection, Session, MessageConsumer everytime a message is found inside the queue because there will be at least 1 message per second inside the queue and just opening/closing resources would not be very efficient (right?). With regards to closing the Transaction object, will that be alright? Or should I keep the transaction open as long as the application is running?import java.util.Date;import javax.jms.Connection;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageConsumer;import javax.jms.MessageListener;import javax.jms.Session;import javax.jms.TextMessage;import net.mycompany.SomeDaoObject;import net.mycompany.SomeDomainObject;import net.mycompany.util.HibernateUtil;import net.mycompany.util.SomeProperties;import org.apache.activemq.ActiveMQConnectionFactory;import org.apache.log4j.Logger;import org.hibernate.Transaction;import com.google.gson.Gson;public class Consumer implements Runnable, MessageListener{    private static final Logger logger = Logger.getLogger(Consumer.class);    private static final Gson gson = new Gson();    private static Connection connection;    private MessageConsumer consumer;    private Session session;    private SomeDaoObject someDaoObject = new SomeDaoObject();    public Consumer() throws Exception {        this.session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);        Destination destination = session.createQueue(SomeProperties.queueName());        this.consumer = session.createConsumer(destination);        this.consumer.setMessageListener(this);    }    @Override    public void run() {        try {            //Start the connection            logger.info(Connection starting...);            connection.start();        }        catch (JMSException ex){            logger.error(ex.getMessage(), ex);        }    }    @Override    public void onMessage(Message message) {        if (message instanceof TextMessage) {            TextMessage txtMessage = (TextMessage) message;            Transaction transaction = null;            try {                SomeDomainObject object= gson.fromJson(txtMessage.getText(), SomeDomainObject.class);                object.setCreateTimestamp(new Date());                object.setCreateUsername(CONSUMER);                logger.info(String.format(Saving object... %s, object));                transaction = HibernateUtil.getCurrentSession().beginTransaction();                someDaoObject.save(object);                transaction.commit();                session.commit();            } catch (JMSException e) {                logger.error(e.getMessage(), e);                try {                    session.rollback();                } catch (JMSException e1) {                  logger.error(e1.getMessage(), e1);                }            } catch (Exception ex) {                logger.error(ex.getMessage(), ex);                if (transaction != null)                    transaction.rollback();            }            finally {                HibernateUtil.closeCurrentSession();            }        }    }    public static void main(String[] args){        try {            HibernateUtil.setConnectionParameters(SomeProperties.blvdDbConnectionUrl(),                    SomeProperties.blvdDbUsername(),                    SomeProperties.blvdDbPassword(),                    SomeProperties.blvdDbShowSql());        } catch (Exception e1) {            logger.error(e1.getMessage(), e1);            System.exit(1);        }        ActiveMQConnectionFactory connectionFactory = null;        try {            connectionFactory = new ActiveMQConnectionFactory(SomeProperties.brokerUrl());        }        catch (Exception ex){            logger.error(ex.getMessage(), ex);            System.exit(1);        }        try {            connection = connectionFactory.createConnection();            new Thread(new Consumer()).start();        } catch (JMSException e) {            logger.error(e.getMessage(), e);            System.exit(1);        } catch (Exception e) {            logger.error(e.getMessage(), e);            System.exit(1);        }        addShutdownHook();    }    private static void addShutdownHook() {        final Thread shutdownHook = new Thread(new Runnable(){            @Override            public void run() {                logger.info(Shutting down...);                if (connection != null) {                    try {                        connection.close();                    } catch (JMSException e) {                        logger.error(e.getMessage(), e);                    }                }            }        });        Runtime.getRuntime().addShutdownHook(shutdownHook);    }}
JMS consumer for message retrieval and listening
java;hibernate;jms