Example usage for javax.jms BytesMessage getBodyLength

List of usage examples for javax.jms BytesMessage getBodyLength

Introduction

In this page you can find the example usage for javax.jms BytesMessage getBodyLength.

Prototype


long getBodyLength() throws JMSException;

Source Link

Document

Gets the number of bytes of the message body when the message is in read-only mode.

Usage

From source file:org.wso2.carbon.event.input.adaptor.jms.internal.util.JMSMessageListener.java

public void onMessage(Message message) {
    try {//  w ww  .  j av a  2s  . com
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        if (message != null) {

            if (log.isDebugEnabled()) {
                log.debug(message);
            }

            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                // Send the text of the message. Conversion to any type (XML,JSON) should
                // not happen here since this message will be built later.
                try {
                    String msgText = textMessage.getText();
                    eventAdaptorListener.onEventCall(msgText);
                } catch (JMSException e) {
                    if (log.isErrorEnabled()) {
                        log.error("Failed to get text from " + textMessage, e);
                    }
                } catch (InputEventAdaptorEventProcessingException e) {
                    if (log.isErrorEnabled()) {
                        log.error(e);
                    }
                }
            } else if (message instanceof MapMessage) {
                MapMessage mapMessage = (MapMessage) message;
                Map event = new HashMap();
                try {
                    Enumeration names = mapMessage.getMapNames();
                    Object name;
                    while (names.hasMoreElements()) {
                        name = names.nextElement();
                        event.put(name, mapMessage.getObject((String) name));
                    }
                    eventAdaptorListener.onEventCall(event);

                } catch (JMSException e) {
                    log.error("Can not read the map message ", e);
                } catch (InputEventAdaptorEventProcessingException e) {
                    log.error("Can not send the message to broker ", e);
                }
            } else if (message instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) message;
                byte[] bytes;
                bytes = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(bytes);
                eventAdaptorListener.onEventCall(new String(bytes, "UTF-8"));
            } else {
                log.warn("Event dropped due to unsupported message type");
            }
        } else {
            log.warn("Dropping the empty/null event received through jms adaptor");
        }
    } catch (JMSException e) {
        log.error(e);
    } catch (UnsupportedEncodingException e) {
        log.error(e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}

From source file:org.wso2.carbon.registry.caching.invalidator.connection.JMSNotification.java

@Override
public void onMessage(Message message) {
    BytesMessage bytesMessage = (BytesMessage) message;
    byte[] data;/*w ww  . j a  va2s . co  m*/
    try {
        data = new byte[(int) bytesMessage.getBodyLength()];
        for (int i = 0; i < (int) bytesMessage.getBodyLength(); i++) {
            data[i] = bytesMessage.readByte();
        }
        log.debug("Cache invalidation message received: " + new String(data));
    } catch (JMSException jmsException) {
        log.error("Error while reading the received message", jmsException);
        return;
    }

    boolean isCoordinator = false;
    if (CacheInvalidationDataHolder.getConfigContext() != null) {
        isCoordinator = CacheInvalidationDataHolder.getConfigContext().getAxisConfiguration()
                .getClusteringAgent().isCoordinator();
    }
    if (isCoordinator) {
        PrivilegedCarbonContext.startTenantFlow();
        try {
            log.debug("Global cache invalidation: deserializing data to object");
            GlobalCacheInvalidationEvent event = (GlobalCacheInvalidationEvent) deserialize(data);
            log.debug("Global cache invalidation: deserializing complete");
            if (!ConfigurationManager.getSentMsgBuffer().contains(event.getUuid().trim())) { // Ignore own messages
                PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(event.getTenantId(), true);
                CacheManager cacheManager = Caching.getCacheManagerFactory()
                        .getCacheManager(event.getCacheManagerName());
                if (cacheManager != null) {
                    if (cacheManager.getCache(event.getCacheName()) != null) {
                        cacheManager.getCache(event.getCacheName()).remove(event.getCacheKey());
                        log.debug("Global cache invalidated: " + event.getCacheKey());
                    } else {
                        log.error("Global cache invalidation: error cache is null");
                    }
                } else {
                    log.error("Global cache invalidation: error cache manager is null");
                }
            } else {
                // To resolve future performance issues
                ConfigurationManager.getSentMsgBuffer().remove(event.getUuid().trim());
                log.debug("Global cache invalidation: own message ignored");
            }
        } catch (Exception e) {
            log.error("Global cache invalidation: error local cache update", e);
        } finally {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}