Example usage for javax.jms BytesMessage writeBytes

List of usage examples for javax.jms BytesMessage writeBytes

Introduction

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

Prototype


void writeBytes(byte[] value) throws JMSException;

Source Link

Document

Writes a byte array to the bytes message stream.

Usage

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

private Message createBytesMessageFromText(SubmitContext submitContext, String requestContent, Session session)
        throws JMSException {
    BytesMessage bytesMessage = session.createBytesMessage();
    bytesMessage.writeBytes(requestContent.getBytes());
    return bytesMessage;
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

private Message createBytesMessage(Request request, Session session) {
    try {//from  ww  w.java  2  s .c o  m
        InputStream in = request.getAttachments()[0].getInputStream();
        int buff = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((buff = in.read()) != -1) {
            baos.write(buff);
        }
        BytesMessage bytesMessage = session.createBytesMessage();
        bytesMessage.writeBytes(baos.toByteArray());
        return bytesMessage;
    } catch (Exception e) {
        SoapUI.logError(e);
    }
    return null;
}

From source file:com.adaptris.core.jms.BytesMessageTranslator.java

/**
 * <p>/*ww  w.j a v a 2  s .co  m*/
 * Translates an <code>AdaptrisMessage</code> into a <code>BytesMessage</code>.
 * </p>
 * 
 * @param msg the <code>AdaptrisMessage</code> to translate
 * @return a new <code>BytesMessage</code>
 * @throws JMSException
 */
public Message translate(AdaptrisMessage msg) throws JMSException {
    BytesMessage jmsMsg = session.createBytesMessage();
    if (msg.getSize() > streamThreshold()) {
        try (InputStream in = msg.getInputStream();
                BytesMessageOutputStream out = new BytesMessageOutputStream(jmsMsg)) {
            StreamUtil.copyAndClose(in, out);
        } catch (IOException e) {
            throw JmsUtils.wrapJMSException(e);
        }
    } else {
        jmsMsg.writeBytes(msg.getPayload());
    }
    return helper.moveMetadata(msg, jmsMsg);
}

From source file:org.bremersee.common.jms.DefaultJmsConverter.java

private Message createSerializedMessage(Serializable object, Session session) throws JMSException {
    try {/*from   w  w w  .  ja  v  a2 s .  c o m*/
        BytesMessage msg = session.createBytesMessage();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
        msg.writeBytes(outputStream.toByteArray());
        msg.setJMSType(object.getClass().getName());
        return msg;

    } catch (Throwable t) { // NOSONAR
        log.info("Creating Serialized JMS from object of type ["
                + (object == null ? "null" : object.getClass().getName()) + "] failed.");
        return null;
    }
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

@Override
public void process(Tx tx) {
    try {//from  w  w w  .  j a v  a  2s .  c om
        Transaction transaction = toBCSAPITransaction(tx);
        BytesMessage m = session.createBytesMessage();
        m.writeBytes(transaction.toProtobuf().toByteArray());
        transactionProducer.send(m);

        synchronized (correlationBloomFilter) {
            for (Map.Entry<String, BloomFilter> e : correlationBloomFilter.entrySet()) {
                if (tx.passesFilter(e.getValue())) {
                    correlationProducer.get(e.getKey()).send(m);
                }
            }
        }
    } catch (Exception e) {
        log.error("Can not send JMS message ", e);
    }
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

@Override
public void trunkUpdate(final List<Blk> removed, final List<Blk> extended) {
    try {//from   ww  w .jav a  2  s  .  c  om
        List<Block> r = new ArrayList<Block>();
        List<Block> a = new ArrayList<Block>();
        for (Blk blk : removed) {
            WireFormat.Writer writer = new WireFormat.Writer();
            blk.toWire(writer);
            r.add(Block.fromWire(new WireFormat.Reader(writer.toByteArray())));
        }
        for (Blk blk : extended) {
            WireFormat.Writer writer = new WireFormat.Writer();
            blk.toWire(writer);
            a.add(Block.fromWire(new WireFormat.Reader(writer.toByteArray())));
        }
        TrunkUpdateMessage tu = new TrunkUpdateMessage(a, r);
        BytesMessage m = session.createBytesMessage();
        m.writeBytes(tu.toProtobuf().toByteArray());
        trunkProducer.send(m);
    } catch (Exception e) {
        log.error("Can not send JMS message ", e);
    }
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void reply(Destination destination, byte[] msg) {
    MessageProducer replier = null;/*w w w  . ja  v a  2 s  . c om*/
    try {
        replier = session.createProducer(destination);
        BytesMessage m = session.createBytesMessage();
        if (msg != null) {
            m.writeBytes(msg);
        }
        replier.send(m);
    } catch (JMSException e) {
        log.trace("can not reply", e);
    } finally {
        try {
            replier.close();
        } catch (JMSException e) {
        }
    }
}

From source file:hermes.impl.DefaultXMLHelper.java

public Message createMessage(MessageFactory hermes, XMLMessage message)
        throws JMSException, IOException, ClassNotFoundException, DecoderException {
    try {//from ww  w.ja v a 2 s  .  c o  m
        Message rval = hermes.createMessage();

        if (message instanceof XMLTextMessage) {
            rval = hermes.createTextMessage();

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

            if (BASE64_CODEC.equals(textMessage.getCodec())) {
                byte[] bytes = base64EncoderTL.get().decode(textMessage.getText().getBytes());
                textRval.setText(new String(bytes, "ASCII"));
            } else {
                textRval.setText(textMessage.getText());
            }
        } else if (message instanceof XMLMapMessage) {
            rval = hermes.createMapMessage();

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

            for (Iterator iter = mapMessage.getBodyProperty().iterator(); iter.hasNext();) {
                final Property property = (Property) iter.next();

                if (property.getValue() == null) {
                    mapRval.setObject(property.getName(), null);
                } else if (property.getType().equals(String.class.getName())) {
                    mapRval.setString(property.getName(), property.getValue());
                } else if (property.getType().equals(Long.class.getName())) {
                    mapRval.setLong(property.getName(), Long.parseLong(property.getValue()));
                } else if (property.getType().equals(Double.class.getName())) {
                    mapRval.setDouble(property.getName(), Double.parseDouble(property.getValue()));
                } else if (property.getType().equals(Boolean.class.getName())) {
                    mapRval.setBoolean(property.getName(), Boolean.getBoolean(property.getValue()));
                } else if (property.getType().equals(Character.class.getName())) {
                    mapRval.setChar(property.getName(), property.getValue().charAt(0));
                } else if (property.getType().equals(Short.class.getName())) {
                    mapRval.setShort(property.getName(), Short.parseShort(property.getValue()));
                } else if (property.getType().equals(Integer.class.getName())) {
                    mapRval.setInt(property.getName(), Integer.parseInt(property.getValue()));
                }
            }
        } else if (message instanceof XMLBytesMessage) {
            rval = hermes.createBytesMessage();

            XMLBytesMessage bytesMessage = (XMLBytesMessage) message;
            BytesMessage bytesRval = (BytesMessage) rval;

            bytesRval.writeBytes(base64EncoderTL.get().decode(bytesMessage.getBytes().getBytes()));
        } else if (message instanceof XMLObjectMessage) {
            rval = hermes.createObjectMessage();

            XMLObjectMessage objectMessage = (XMLObjectMessage) message;
            ObjectMessage objectRval = (ObjectMessage) rval;
            ByteArrayInputStream bistream = new ByteArrayInputStream(
                    base64EncoderTL.get().decode(objectMessage.getObject().getBytes()));

            ObjectInputStream oistream = new ObjectInputStream(bistream);

            objectRval.setObject((Serializable) oistream.readObject());
        }

        //
        // JMS Header properties

        try {
            rval.setJMSDeliveryMode(message.getJMSDeliveryMode());
        } catch (JMSException ex) {
            log.error("unable to set JMSDeliveryMode to " + message.getJMSDeliveryMode() + ": "
                    + ex.getMessage());
        }

        try {
            rval.setJMSMessageID(message.getJMSMessageID());
        } catch (JMSException ex) {
            log.error("unable to set JMSMessageID: " + ex.getMessage(), ex);
        }

        try {
            if (message.getJMSExpiration() != null) {
                rval.setJMSExpiration(message.getJMSExpiration());
            }
        } catch (JMSException ex) {
            log.error("unable to set JMSExpiration: " + ex.getMessage(), ex);
        }

        try {
            if (message.getJMSPriority() != null) {
                rval.setJMSPriority(message.getJMSPriority());
            }
        } catch (JMSException ex) {
            log.error("unable to set JMSPriority: " + ex.getMessage(), ex);
        }

        try {
            if (message.getJMSTimestamp() != null) {
                rval.setJMSTimestamp(message.getJMSTimestamp());
            }
        } catch (JMSException ex) {
            log.error("unable to set JMSTimestamp:" + ex.getMessage(), ex);
        }

        if (message.getJMSCorrelationID() != null) {
            rval.setJMSCorrelationID(message.getJMSCorrelationID());
        }

        if (message.getJMSReplyTo() != null && !message.getJMSReplyTo().equals("null")) {
            rval.setJMSReplyTo(hermes.getDestination(message.getJMSReplyTo(),
                    Domain.getDomain(message.getJMSReplyToDomain())));
        }

        if (message.getJMSType() != null) {
            rval.setJMSType(message.getJMSType());
        }

        if (message.getJMSDestination() != null) {
            if (message.isFromQueue()) {
                rval.setJMSDestination(hermes.getDestination(message.getJMSDestination(), Domain.QUEUE));
            } else {
                rval.setJMSDestination(hermes.getDestination(message.getJMSDestination(), Domain.TOPIC));
            }
        }

        for (Iterator iter = message.getHeaderProperty().iterator(); iter.hasNext();) {
            Property property = (Property) iter.next();

            if (property.getValue() == null) {
                rval.setObjectProperty(property.getName(), null);
            } else if (property.getType().equals(String.class.getName())) {
                rval.setStringProperty(property.getName(), StringEscapeUtils.unescapeXml(property.getValue()));
            } else if (property.getType().equals(Long.class.getName())) {
                rval.setLongProperty(property.getName(), Long.parseLong(property.getValue()));
            } else if (property.getType().equals(Double.class.getName())) {
                rval.setDoubleProperty(property.getName(), Double.parseDouble(property.getValue()));
            } else if (property.getType().equals(Boolean.class.getName())) {
                rval.setBooleanProperty(property.getName(), Boolean.parseBoolean(property.getValue()));
            } else if (property.getType().equals(Short.class.getName())) {
                rval.setShortProperty(property.getName(), Short.parseShort(property.getValue()));
            } else if (property.getType().equals(Integer.class.getName())) {
                rval.setIntProperty(property.getName(), Integer.parseInt(property.getValue()));
            }
        }

        return rval;
    } catch (NamingException e) {
        throw new HermesException(e);
    }

}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addBloomScanListener() throws JMSException {
    addMessageListener("scanRequest", new MessageListener() {
        @Override/*from   w  w w  .  j  a  v a  2  s . c  om*/
        public void onMessage(Message msg) {
            BytesMessage o = (BytesMessage) msg;
            byte[] body;
            try {
                body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                BCSAPIMessage.FilterRequest request = BCSAPIMessage.FilterRequest.parseFrom(body);
                byte[] data = request.getFilter().toByteArray();
                long hashFunctions = request.getHashFunctions();
                long tweak = request.getTweak();
                UpdateMode updateMode = UpdateMode.values()[request.getMode()];
                final BloomFilter filter = new BloomFilter(data, hashFunctions, tweak, updateMode);
                final MessageProducer producer = session.createProducer(msg.getJMSReplyTo());
                requestProcessor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            store.scan(filter, new TransactionProcessor() {
                                @Override
                                public void process(Tx tx) {
                                    if (tx != null) {
                                        Transaction transaction = toBCSAPITransaction(tx);
                                        BytesMessage m;
                                        try {
                                            m = session.createBytesMessage();
                                            m.writeBytes(transaction.toProtobuf().toByteArray());
                                            producer.send(m);
                                        } catch (JMSException e) {
                                        }
                                    } else {
                                        try {
                                            BytesMessage m = session.createBytesMessage();
                                            producer.send(m); // indicate EOF
                                            producer.close();
                                        } catch (JMSException e) {
                                        }
                                    }
                                }
                            });
                        } catch (ValidationException e) {
                            log.error("Error while scanning", e);
                        }
                    }
                });
            } catch (JMSException e) {
                log.error("invalid filter request", e);
            } catch (InvalidProtocolBufferException e) {
                log.error("invalid filter request", e);
            }
        }
    });
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addMatchScanListener() throws JMSException {
    addMessageListener("matchRequest", new MessageListener() {
        @Override//from   ww w  . j av a 2  s  . c  om
        public void onMessage(Message msg) {
            BytesMessage o = (BytesMessage) msg;
            byte[] body;
            try {
                body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                BCSAPIMessage.ExactMatchRequest request = BCSAPIMessage.ExactMatchRequest.parseFrom(body);
                final List<byte[]> match = new ArrayList<byte[]>();
                for (ByteString bs : request.getMatchList()) {
                    match.add(bs.toByteArray());
                }
                final UpdateMode mode = UpdateMode.values()[request.getMode()];
                final MessageProducer producer = session.createProducer(msg.getJMSReplyTo());
                final long after = request.hasAfter() ? request.getAfter() : 0;
                requestProcessor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            store.filterTransactions(match, mode, after, new TransactionProcessor() {
                                @Override
                                public void process(Tx tx) {
                                    if (tx != null) {
                                        Transaction transaction = toBCSAPITransaction(tx);
                                        BytesMessage m;
                                        try {
                                            m = session.createBytesMessage();
                                            m.writeBytes(transaction.toProtobuf().toByteArray());
                                            producer.send(m);
                                        } catch (JMSException e) {
                                        }
                                    } else {
                                        try {
                                            BytesMessage m = session.createBytesMessage();
                                            producer.send(m); // indicate EOF
                                            producer.close();
                                        } catch (JMSException e) {
                                        }
                                    }
                                }
                            });
                        } catch (ValidationException e) {
                            log.error("Error while scanning", e);
                        }
                    }
                });
            } catch (JMSException e) {
                log.error("invalid filter request", e);
            } catch (InvalidProtocolBufferException e) {
                log.error("invalid filter request", e);
            }
        }
    });
}