Example usage for javax.jms BytesMessage readByte

List of usage examples for javax.jms BytesMessage readByte

Introduction

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

Prototype


byte readByte() throws JMSException;

Source Link

Document

Reads a signed 8-bit value from the bytes message stream.

Usage

From source file:hermes.impl.DefaultXMLHelper.java

public XMLMessage createXMLMessage(ObjectFactory factory, Message message)
        throws JMSException, IOException, EncoderException {
    try {/*from w  w w  .j  a  va  2s  .  c  o  m*/
        XMLMessage rval = factory.createXMLMessage();

        if (message instanceof TextMessage) {
            rval = factory.createXMLTextMessage();

            XMLTextMessage textRval = (XMLTextMessage) rval;
            TextMessage textMessage = (TextMessage) message;

            if (isBase64EncodeTextMessages()) {
                byte[] bytes = base64EncoderTL.get().encode(textMessage.getText().getBytes());
                textRval.setText(new String(bytes, "ASCII"));
                textRval.setCodec(BASE64_CODEC);
            } else {
                textRval.setText(textMessage.getText());
            }
        } else if (message instanceof MapMessage) {
            rval = factory.createXMLMapMessage();

            XMLMapMessage mapRval = (XMLMapMessage) rval;
            MapMessage mapMessage = (MapMessage) message;

            for (Enumeration iter = mapMessage.getMapNames(); iter.hasMoreElements();) {
                String propertyName = (String) iter.nextElement();
                Object propertyValue = mapMessage.getObject(propertyName);
                Property xmlProperty = factory.createProperty();

                if (propertyValue != null) {
                    xmlProperty.setValue(propertyValue.toString());
                    xmlProperty.setType(propertyValue.getClass().getName());
                }
                xmlProperty.setName(propertyName);

                mapRval.getBodyProperty().add(xmlProperty);
            }
        } else if (message instanceof BytesMessage) {
            rval = factory.createXMLBytesMessage();

            XMLBytesMessage bytesRval = (XMLBytesMessage) rval;
            BytesMessage bytesMessage = (BytesMessage) message;
            ByteArrayOutputStream bosream = new ByteArrayOutputStream();

            bytesMessage.reset();

            try {
                for (;;) {
                    bosream.write(bytesMessage.readByte());
                }
            } catch (MessageEOFException ex) {
                // NOP
            }

            bytesRval.setBytes(new String(base64EncoderTL.get().encode(bosream.toByteArray())));
        } else if (message instanceof ObjectMessage) {
            rval = factory.createXMLObjectMessage();

            XMLObjectMessage objectRval = (XMLObjectMessage) rval;
            ObjectMessage objectMessage = (ObjectMessage) message;

            ByteArrayOutputStream bostream = new ByteArrayOutputStream();
            ObjectOutputStream oostream = new ObjectOutputStream(bostream);

            oostream.writeObject(objectMessage.getObject());
            oostream.flush();
            byte b[] = base64EncoderTL.get().encode(bostream.toByteArray());
            String s = new String(b, "ASCII");
            objectRval.setObject(s);
        }

        if (message.getJMSReplyTo() != null) {
            rval.setJMSReplyTo(JMSUtils.getDestinationName(message.getJMSReplyTo()));
            rval.setJMSReplyToDomain(Domain.getDomain(message.getJMSReplyTo()).getId());
        }

        // try/catch each individually as we sometime find some JMS
        // providers
        // can barf
        try {
            rval.setJMSDeliveryMode(message.getJMSDeliveryMode());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSExpiration(message.getJMSExpiration());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSMessageID(message.getJMSMessageID());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSPriority(message.getJMSPriority());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSRedelivered(message.getJMSRedelivered());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        } catch (IllegalStateException ex) {
            // http://hermesjms.com/forum/viewtopic.php?f=4&t=346

            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSTimestamp(message.getJMSTimestamp());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSType(message.getJMSType());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            rval.setJMSCorrelationID(message.getJMSCorrelationID());
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        try {
            if (message.getJMSDestination() != null) {
                rval.setJMSDestination(JMSUtils.getDestinationName(message.getJMSDestination()));
                rval.setFromQueue(JMSUtils.isQueue(message.getJMSDestination()));
            }
        } catch (JMSException ex) {
            log.error(ex.getMessage(), ex);
        }

        for (final Enumeration iter = message.getPropertyNames(); iter.hasMoreElements();) {
            String propertyName = (String) iter.nextElement();

            if (!propertyName.startsWith("JMS")) {
                Object propertyValue = message.getObjectProperty(propertyName);
                Property property = factory.createProperty();

                property.setName(propertyName);

                if (propertyValue != null) {
                    property.setValue(StringEscapeUtils.escapeXml(propertyValue.toString()));
                    property.setType(propertyValue.getClass().getName());
                }

                rval.getHeaderProperty().add(property);
            }
        }

        return rval;
    } catch (Exception ex) {
        throw new HermesException(ex);
    }
}

From source file:org.codehaus.stomp.StompTest.java

public void testSendMessageWithContentLength() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);//from   ww w.j  a  v a2 s . c  o m

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    byte[] data = new byte[] { 1, 0, 0, 4 };

    frame = "SEND\n" + "destination:/queue/" + getQueueName() + "\n" + "content-length:" + data.length + "\n\n";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(frame.getBytes("UTF-8"));
    baos.write(data);
    baos.write('\0');
    baos.flush();
    sendFrame(baos.toByteArray());

    BytesMessage message = (BytesMessage) consumer.receive(10000);
    Assert.assertNotNull(message);
    assertEquals(data.length, message.getBodyLength());
    assertEquals(data[0], message.readByte());
    assertEquals(data[1], message.readByte());
    assertEquals(data[2], message.readByte());
    assertEquals(data[3], message.readByte());
}

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

@Override
public void onMessage(Message message) {
    BytesMessage bytesMessage = (BytesMessage) message;
    byte[] data;/* www  . ja v  a  2 s.com*/
    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();
        }
    }
}