Example usage for javax.jms Session toString

List of usage examples for javax.jms Session toString

Introduction

In this page you can find the example usage for javax.jms Session toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:it.geosolutions.geoserver.jms.client.JMSQueueListener.java

@Override
public void onMessage(Message message, Session session) throws JMSException {

    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Incoming message event for session: " + session.toString());
    }/*w w  w .ja  va2s .  c o  m*/

    // CHECKING LISTENER STATUS
    if (!isEnabled()) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Incoming message is swallowed since this component is disabled");
        }
        return;
    }
    // FILTERING INCOMING MESSAGE
    if (!message.propertyExists(JMSConfiguration.INSTANCE_NAME_KEY)) {
        throw new JMSException("Unable to handle incoming message, property \'"
                + JMSConfiguration.INSTANCE_NAME_KEY + "\' not set.");
    }

    // FILTERING INCOMING MESSAGE
    if (!message.propertyExists(JMSConfiguration.GROUP_KEY)) {
        throw new JMSException(
                "Unable to handle incoming message, property \'" + JMSConfiguration.GROUP_KEY + "\' not set.");
    }

    // check if message comes from a master with the same name of this slave
    if (message.getStringProperty(JMSConfiguration.INSTANCE_NAME_KEY)
            .equals(config.getConfiguration(JMSConfiguration.INSTANCE_NAME_KEY))) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Incoming message discarded: source is equal to destination");
        }
        // if so discard the message
        return;
    }

    // check if message comes from a different group
    final String group = message.getStringProperty(JMSConfiguration.GROUP_KEY);
    final String localGroup = config.getConfiguration(JMSConfiguration.GROUP_KEY);
    if (!group.equals(localGroup)) {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Incoming message discarded: incoming group-->" + group
                    + " is different from the local one-->" + localGroup);
        }
        // if so discard the message
        return;
    }

    // check the property which define the SPI used (to serialize on the
    // server side).
    if (!message.propertyExists(JMSEventHandlerSPI.getKeyName()))
        throw new JMSException("Unable to handle incoming message, property \'"
                + JMSEventHandlerSPI.getKeyName() + "\' not set.");

    // END -> FILTERING INCOMING MESSAGE

    // get the name of the SPI used to serialize the message
    final String generatorClass = message.getStringProperty(JMSEventHandlerSPI.getKeyName());
    if (generatorClass == null || generatorClass.isEmpty()) {
        throw new IllegalArgumentException("Unable to handle a message without a generator class name");
    }
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine(
                "Incoming message was serialized using an handler generated by: \'" + generatorClass + "\'");
    }

    // USING INCOMING MESSAGE
    if (message instanceof ObjectMessage) {

        final ObjectMessage objMessage = (ObjectMessage) (message);
        final Serializable obj = objMessage.getObject();

        try {
            // lookup the SPI handler, search is performed using the
            // name
            final JMSEventHandler<Serializable, Object> handler = jmsManager
                    .getHandlerByClassName(generatorClass);
            if (handler == null) {
                throw new JMSException("Unable to find SPI named \'" + generatorClass
                        + "\', be shure to load that SPI into your context.");
            }

            final Enumeration<String> keys = message.getPropertyNames();
            final Properties options = new Properties();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                options.put(key, message.getObjectProperty(key));
            }
            handler.setProperties(options);

            // try to synchronize object locally
            if (!handler.synchronize(handler.deserialize(obj))) {
                throw new JMSException("Unable to synchronize message locally.\n SPI: " + generatorClass);
            }

        } catch (Exception e) {
            final JMSException jmsE = new JMSException(e.getLocalizedMessage());
            jmsE.initCause(e);
            throw jmsE;
        }

    } else
        throw new JMSException("Unrecognized message type for catalog incoming event");
}