Example usage for javax.jms BytesMessage readBytes

List of usage examples for javax.jms BytesMessage readBytes

Introduction

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

Prototype


int readBytes(byte[] value) throws JMSException;

Source Link

Document

Reads a byte array from the bytes message stream.

Usage

From source file:org.oxymores.chronix.engine.Runner.java

@Override
public void onMessage(Message msg) {
    if (msg instanceof ObjectMessage) {
        ObjectMessage omsg = (ObjectMessage) msg;
        try {/*from  ww w. j av a  2s .  c  o  m*/
            Object o = omsg.getObject();
            if (o instanceof PipelineJob) {
                PipelineJob pj = (PipelineJob) o;
                log.debug(String.format("Job execution %s request was received", pj.getId()));
                recvPJ(pj);
                jmsCommit();
                return;
            } else if (o instanceof RunResult) {
                RunResult rr = (RunResult) o;
                recvRR(rr);
                jmsCommit();
                return;
            } else {
                log.warn(
                        "An object was received by the Runner that was not of a valid type. It will be ignored.");
                jmsCommit();
                return;
            }

        } catch (JMSException e) {
            log.error(
                    "An error occurred during job reception. Message will stay in queue and will be analysed later",
                    e);
            jmsRollback();
            return;
        }
    } else if (msg instanceof TextMessage) {
        TextMessage tmsg = (TextMessage) msg;
        try {
            recvTextMessage(tmsg);
            jmsCommit();
        } catch (JMSException e) {
            log.error("An error occurred during parameter resolution", e);
            jmsRollback();
            return;
        }
    } else if (msg instanceof BytesMessage) {
        // log file reception
        BytesMessage bmsg = (BytesMessage) msg;
        String fn = "dump.txt";
        try {
            fn = bmsg.getStringProperty("FileName");
        } catch (JMSException e) {
            log.error(
                    "An log file was sent without a FileName property. It will be lost. Will not impact the scheduler itself.",
                    e);
            jmsCommit();
        }

        try {
            int l = (int) bmsg.getBodyLength();
            byte[] r = new byte[l];
            bmsg.readBytes(r);
            IOUtils.write(r, new FileOutputStream(new File(FilenameUtils.concat(this.logDbPath, fn))));
            jmsCommit();
        } catch (Exception e) {
            log.error(
                    "An error has occured while receiving a log file. It will be lost. Will not impact the scheduler itself.",
                    e);
            jmsCommit();
        }
    }
}

From source file:org.skyscreamer.nevado.jms.message.NevadoBytesMessage.java

protected NevadoBytesMessage(BytesMessage message) throws JMSException {
    super(message);
    message.reset();/*from ww w  . j a  v  a 2s  . c o  m*/
    for (int count = 0; count < message.getBodyLength();) {
        byte[] buffer = new byte[10240];
        int numRead = message.readBytes(buffer);
        writeBytes(buffer, 0, numRead);
        count += numRead;
    }
}

From source file:org.springframework.jms.support.converter.obm.MarshallingMessageConverter.java

protected Object unmarshalFromBytesMessage(Class clzz, BytesMessage message,
        org.springframework.obm.Unmarshaller unmarshaller)
        throws JMSException, IOException, XmlMappingException {
    try {/*from  ww w .jav  a2  s .  co m*/
        byte[] bytes = new byte[(int) message.getBodyLength()];
        message.readBytes(bytes);
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Object result = unmarshaller.unmarshal(clzz, bis);
        Assert.notNull(result, "the result from the queue is null");
        if (log.isDebugEnabled()) {
            log.debug("received: " + result);
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

public void onMessage(Message message) {
    try {/*from  www.j  av a  2  s.  c om*/
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
        if (message != null) {

            if (log.isDebugEnabled()) {
                log.debug("Event received in JMS Event Adaptor - " + 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.onEvent(msgText);
                } catch (JMSException e) {
                    if (log.isErrorEnabled()) {
                        log.error("Failed to get text from " + textMessage, e);
                    }
                } catch (InputEventAdapterRuntimeException 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.onEvent(event);

                } catch (JMSException e) {
                    log.error("Can not read the map message ", e);
                } catch (InputEventAdapterRuntimeException 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.onEvent(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);
    } catch (Throwable t) {
        log.error(t);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}

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

public void onMessage(Message message) {
    try {//w  w  w. ja  v a  2 s  . c  o m
        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();
    }
}