gt:             ObjectMessage msg = createObjectMessage();
context: /**
   *
   * Copyright (c) 2009-2015 Freedomotic team http://freedomotic.com
   *
   * This file is part of Freedomotic
   *
   * This Program 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 2, or (at your option) any later version.
   *
   * This Program 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
   * Freedomotic; see the file COPYING. If not, see
   * <http://www.gnu.org/licenses/>.
   */
  package com.freedomotic.bus;
  
  import com.freedomotic.api.EventTemplate;
  import com.freedomotic.app.Freedomotic;
  import com.freedomotic.app.Profiler;
  import com.freedomotic.reactions.Command;
  import com.freedomotic.settings.AppConfig;
  import com.google.inject.Injector;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.inject.Inject;
  import javax.jms.DeliveryMode;
  import javax.jms.Destination;
  import javax.jms.JMSException;
  import javax.jms.Message;
  import javax.jms.MessageConsumer;
  import javax.jms.MessageProducer;
  import javax.jms.ObjectMessage;
  import javax.jms.Queue;
  import javax.jms.Session;
  import javax.jms.TemporaryQueue;
  import javax.jms.Topic;
  import org.apache.activemq.command.ActiveMQQueue;
  
  /**
   * Bus services implementation.
   * <p>
   * It is life cycle managed, see {@link LifeCycle}
   *
   * @author Freedomotic Team
   *
   */
  final class BusServiceImpl extends LifeCycle implements BusService {
  
      private static final Logger LOG = Logger.getLogger(BusServiceImpl.class.getName());
  
      //private AppConfig config;
      private BusBroker brokerHolder;
      private BusConnection connectionHolder;
      private Session receiveSession;
      private Session sendSession;
      private Session unlistenedSession;
      private AppConfig conf;
      private Injector injector;
      protected MessageProducer messageProducer;
  
      @Inject
      public BusServiceImpl(AppConfig config, Injector inj) {
          //this.config = config;
          if (BootStatus.getCurrentStatus() == BootStatus.STOPPED) {
              conf = config;
              injector = inj;
              init();
              if (sendSession == null) {
                  throw new IllegalStateException("Messaging bus has not yet a valid send session");
              }
          }
          LOG.info("Messaging bus is " + BootStatus.getCurrentStatus().name());
      }
  
      /**
       * {@inheritDoc}
       *
       * @throws java.lang.Exception
       */
      @Override
      protected void start() throws Exception {
  
          BootStatus.setCurrentStatus(BootStatus.BOOTING);
  
          brokerHolder = new BusBroker();
          brokerHolder.init();
  
          connectionHolder = injector.getInstance(BusConnection.class);
          connectionHolder.init();
  
          receiveSession = createSession();
          // an unlistened session
          unlistenedSession = createSession();
  
          sendSession = createSession();
          // null parameter creates a producer with no specified destination
          messageProducer = createMessageProducer();
  
          BootStatus.setCurrentStatus(BootStatus.STARTED);
      }
  
      private MessageProducer createMessageProducer() throws JMSException {
  
          // null parameter creates a producer with no specified destination
          final MessageProducer createdProducer = sendSession.createProducer(null);
  
          // configure
          createdProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
          //final int tiemToLive = config.getIntProperty("KEY_MESSAGES_TTL", 1000);
          //createProducer.setTimeToLive(tiemToLive);
  
          return createdProducer;
      }
  
      /**
       * {@inheritDoc}
       *
       * @throws java.lang.Exception
       */
      @Override
      protected void stop() throws Exception {
  
          BootStatus.setCurrentStatus(BootStatus.STOPPING);
  
          messageProducer.close();
          closeSession(sendSession);
  
          closeSession(unlistenedSession);
          closeSession(receiveSession);
  
          connectionHolder.destroy();
          brokerHolder.destroy();
  
          BootStatus.setCurrentStatus(BootStatus.STOPPED);
      }
  
      /**
       * {@inheritDoc}
       */
      // TODO Freedomotic.java needs this method publicly visible. A whole repackage is needed.  
      @Override
      public void destroy() {
          super.destroy();
      }
  
      /**
       * {@inheritDoc}
       */
      // TODO Freedomotic.java needs this method publicly visible. A whole repackage is needed.  
      @Override
      public void init() {
          super.init();
      }
  
      private void closeSession(final Session session) throws Exception {
  
          session.close();
      }
  
      @Override
      public Session createSession() throws Exception {
          return connectionHolder.createSession();
  
      }
  
      private MessageProducer getMessageProducer() {
  
          return messageProducer;
      }
  
      /**
       * {@inheritDoc}
       *
       * @return
       */
      @Override
      public Session getReceiveSession() {
          return receiveSession;
      }
  
      /**
       * {@inheritDoc}
       *
       * @return
       */
      @Override
      public Session getSendSession() {
          return sendSession;
      }
  
      /**
       * {@inheritDoc}
       *
       * @return
       */
      @Override
      public Session getUnlistenedSession() {
          return unlistenedSession;
      }
  
      private ObjectMessage createObjectMessage() throws JMSException {
          return getSendSession().createObjectMessage();
      }
  
      /**
       * {@inheritDoc}
       */
      @Override
      public void reply(Command command, Destination destination, String correlationID) {
          if (destination == null) {
              throw new IllegalArgumentException("Null reply destination "
                      + "for command " + command.getName() + " "
                      + "(reply timeout: " + command.getReplyTimeout() + ")");
          }
          try {
  