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:drepcap.frontend.jms.JmsAdapter.java

public void startReceiveData() throws JMSException {
    dataTopic = session.createTopic(componentName + ".data");
    dataConsumer = session.createConsumer(dataTopic);
    dataConsumer.setMessageListener(new MessageListener() {
        @Override//from   w  w  w .  ja  va2 s  .co m
        public void onMessage(Message msg) {
            for (ByteArrayReceiver baDataReceiver : byteArrayDataReceivers) {
                if (baDataReceiver != null && msg instanceof BytesMessage) {
                    BytesMessage bMsg = (BytesMessage) msg;
                    try {
                        byte[] data = new byte[(int) bMsg.getBodyLength()];
                        bMsg.readBytes(data);
                        baDataReceiver.process(data);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }

            for (ObjectReceiver objReceiver : objectReceivers) {
                if (objReceiver != null && msg instanceof ObjectMessage) {
                    ObjectMessage objMsg = (ObjectMessage) msg;
                    try {
                        objReceiver.process(objMsg.getObject());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
}

From source file:com.kinglcc.spring.jms.core.converter.Jackson2JmsMessageConverter.java

/**
 * Convert a BytesMessage to a Java Object with the specified type.
 * @param message the input message/*w  w w  .ja  v a  2s .  c  om*/
 * @return the message body
 * @throws JMSException if thrown by JMS
 * @throws IOException in case of I/O errors
 */
protected String getPayloadFromBytesMessage(BytesMessage message) throws JMSException, IOException {

    String encoding = this.encoding;
    if (this.encodingPropertyName != null && message.propertyExists(this.encodingPropertyName)) {
        encoding = message.getStringProperty(this.encodingPropertyName);
    }
    byte[] bytes = new byte[(int) message.getBodyLength()];
    message.readBytes(bytes);
    try {
        return new String(bytes, encoding);
    } catch (UnsupportedEncodingException ex) {
        throw new MessageConversionException("Cannot convert bytes to String", ex);
    }
}

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

private void addTransactionRequestListener() throws JMSException {
    addMessageListener("transactionRequest", new MessageListener() {
        @Override/* w ww .j  a va2  s  . com*/
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString();
                Transaction t = getTransaction(hash);
                if (t != null) {
                    reply(o.getJMSReplyTo(), t.toProtobuf().toByteArray());
                } else {
                    reply(o.getJMSReplyTo(), null);
                }
            } catch (Exception e) {
                log.trace("Rejected invalid transaction request ", e);
            }
        }
    });
}

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

private void addBlockrequestListener() throws JMSException {
    addMessageListener("blockRequest", new MessageListener() {
        @Override/*from  w w w.  j a  v  a2s . c o  m*/
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString();
                Block b = getBlock(hash);
                if (b != null) {
                    reply(o.getJMSReplyTo(), b.toProtobuf().toByteArray());
                } else {
                    reply(o.getJMSReplyTo(), null);
                }
            } catch (Exception e) {
                log.trace("Rejected invalid block request ", e);
            }
        }
    });
}

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

private void addColorRequestListener() throws JMSException {
    addMessageListener("colorRequest", new MessageListener() {
        @Override//from   w ww.j av a2 s. c  om
        public void onMessage(Message message) {
            BytesMessage o = (BytesMessage) message;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString();
                Color c = getColor(hash);
                if (c != null) {
                    reply(o.getJMSReplyTo(), c.toProtobuf().toByteArray());
                } else {
                    reply(o.getJMSReplyTo(), null);
                }
            } catch (Exception e) {
                log.trace("Rejected invalid color request ", e);
            }
        }
    });
}

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

private void addNewBlockListener() throws JMSException {
    addMessageListener("newBlock", new MessageListener() {
        @Override/*from w  w w. ja  v a 2s  .  co m*/
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                Block block = Block.fromProtobuf(BCSAPIMessage.Block.parseFrom(body));
                block.computeHash();
                sendBlock(block);
                reply(o.getJMSReplyTo(), null);
            } catch (Exception e) {
                BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder();
                builder.setBcsapiversion(1);
                builder.addMessage(e.getMessage());
                try {
                    reply(o.getJMSReplyTo(), builder.build().toByteArray());
                } catch (JMSException e1) {
                    log.error("Can not send reply ", e1);
                }
            }
        }
    });
}

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

private void addNewTransactionListener() throws JMSException {
    addMessageListener("newTransaction", new MessageListener() {
        @Override//w  w  w  .  j av  a  2s .  c o  m
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                Transaction transaction = Transaction.fromProtobuf(BCSAPIMessage.Transaction.parseFrom(body));
                transaction.computeHash();
                sendTransaction(transaction);
                reply(o.getJMSReplyTo(), null);
            } catch (Exception e) {
                BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder();
                builder.setBcsapiversion(1);
                builder.addMessage(e.getMessage());
                try {
                    reply(o.getJMSReplyTo(), builder.build().toByteArray());
                } catch (JMSException e1) {
                    log.error("Can not send reply ", e1);
                }
            }
        }
    });
}

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

private void addNewColorListener() throws JMSException {
    addMessageListener("newColor", new MessageListener() {
        @Override//  www  .  j av a  2  s  .  c om
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                Color color = Color.fromProtobuf(BCSAPIMessage.Color.parseFrom(body));
                StoredColor sc = new StoredColor();
                sc.setFungibleName(color.getFungibleName());
                sc.setExpiryHeight(color.getExpiryHeight());
                sc.setPubkey(color.getPubkey());
                sc.setSignature(color.getSignature());
                sc.setTerms(color.getTerms());
                sc.setUnit(color.getUnit());
                sc.setTxHash(color.getTransaction());
                store.issueColor(sc);
                reply(o.getJMSReplyTo(), null);
            } catch (Exception e) {
                BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder();
                builder.setBcsapiversion(1);
                builder.addMessage(e.getMessage());
                try {
                    reply(o.getJMSReplyTo(), builder.build().toByteArray());
                } catch (JMSException e1) {
                    log.error("Can not send reply ", e1);
                }
            }
        }
    });
}

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

private void addBloomFilterListener() throws JMSException {
    addMessageListener("filterRequest", new MessageListener() {
        @Override/*w w w . j a v a2 s  .  c o m*/
        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()];
                BloomFilter filter = new BloomFilter(data, hashFunctions, tweak, updateMode);
                synchronized (correlationBloomFilter) {
                    if (!correlationProducer.containsKey(o.getJMSCorrelationID())) {
                        MessageProducer producer = session.createProducer(msg.getJMSReplyTo());
                        correlationProducer.put(o.getJMSCorrelationID(), producer);
                    }
                    correlationBloomFilter.put(o.getJMSCorrelationID(), filter);
                }
            } 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 addBloomScanListener() throws JMSException {
    addMessageListener("scanRequest", new MessageListener() {
        @Override/* w  ww.j  a  va2  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);
            }
        }
    });
}