当前位置: 首页>>代码示例>>Java>>正文


Java BytesMessage.getBodyLength方法代码示例

本文整理汇总了Java中javax.jms.BytesMessage.getBodyLength方法的典型用法代码示例。如果您正苦于以下问题:Java BytesMessage.getBodyLength方法的具体用法?Java BytesMessage.getBodyLength怎么用?Java BytesMessage.getBodyLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.jms.BytesMessage的用法示例。


在下文中一共展示了BytesMessage.getBodyLength方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onMessage

import javax.jms.BytesMessage; //导入方法依赖的package包/类
@Override
public void onMessage(Message message) {

  try {

    if (message instanceof BytesMessage) {

      BytesMessage bytesMessage = (BytesMessage) message;
      byte[] data = new byte[(int) bytesMessage.getBodyLength()];
      bytesMessage.readBytes(data);
      LOG.info("Message received {}", new String(data));

    } else if (message instanceof TextMessage) {

      TextMessage textMessage = (TextMessage) message;
      String text = textMessage.getText();
      LOG.info("Message received {}", text);
    }

  } catch (JMSException jmsEx) {
    jmsEx.printStackTrace();
  }
}
 
开发者ID:ppatierno,项目名称:amqp-kafka-demo,代码行数:24,代码来源:Receiver.java

示例2: convertFromBytesMessage

import javax.jms.BytesMessage; //导入方法依赖的package包/类
/**
 * Convert a BytesMessage to a Java Object with the specified type.
 * @param message the input message
 * @param targetJavaType the target type
 * @return the message converted to an object
 * @throws JMSException if thrown by JMS
 * @throws IOException in case of I/O errors
 */
protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType)
		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 {
		String body = new String(bytes, encoding);
		return this.objectMapper.readValue(body, targetJavaType);
	}
	catch (UnsupportedEncodingException ex) {
		throw new MessageConversionException("Cannot convert bytes to String", ex);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:MappingJackson2MessageConverter.java

示例3: getPayload

import javax.jms.BytesMessage; //导入方法依赖的package包/类
private String getPayload(Message message) throws Exception {
	String payload = null;
	
	if (message instanceof TextMessage) {
		payload = ((TextMessage) message).getText();
	} 
	else if(message instanceof BytesMessage) {
		BytesMessage bMessage = (BytesMessage) message;
		int payloadLength = (int)bMessage.getBodyLength();
		byte payloadBytes[] = new byte[payloadLength];
		bMessage.readBytes(payloadBytes);
		payload = new String(payloadBytes);
	}
	else {
		log.warn("Message not recognized as a TextMessage or BytesMessage.  It is of type: "+message.getClass().toString());
		payload = message.toString();
	}
	return payload;
}
 
开发者ID:sdeeg-pivotal,项目名称:rabbitmq-jms-samples,代码行数:20,代码来源:SimpleMessageListener.java

示例4: run

import javax.jms.BytesMessage; //导入方法依赖的package包/类
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
    while (runningChecker.isRunning()) {
        exceptionListener.checkErroneous();

        Message message = consumer.receive(1000);
        if (! (message instanceof BytesMessage)) {
            LOG.warn("Active MQ source received non bytes message: {}", message);
            return;
        }
        BytesMessage bytesMessage = (BytesMessage) message;
        byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
        bytesMessage.readBytes(bytes);
        OUT value = deserializationSchema.deserialize(bytes);
        synchronized (ctx.getCheckpointLock()) {
            ctx.collect(value);
            if (!autoAck) {
                addId(bytesMessage.getJMSMessageID());
                unacknowledgedMessages.put(bytesMessage.getJMSMessageID(), bytesMessage);
            }
        }
    }
}
 
开发者ID:apache,项目名称:bahir-flink,代码行数:24,代码来源:AMQSource.java

示例5: onMessage

import javax.jms.BytesMessage; //导入方法依赖的package包/类
@Override
public void onMessage(Message message) {
    try {
        if (message instanceof BytesMessage) {
            Destination source = message.getJMSDestination();
            BytesMessage bytesMsg = (BytesMessage) message;

            byte[] payload = new byte[(int) bytesMsg.getBodyLength()];
            bytesMsg.readBytes(payload);

            listeners.forEach(listener -> {
                listener.onMessage(source.toString(), payload);
            });
        } else {
            LOG.debug("Received message type we don't yet handle: {}", message);
        }

        // TODO - Handle other message types.

    } catch (Exception ex) {
        LOG.error("Error delivering incoming message to listeners: {}", ex.getMessage());
        LOG.trace("Error detail", ex);
    }
}
 
开发者ID:rhiot,项目名称:iot-spec,代码行数:25,代码来源:QpidJMSTransport.java

示例6: extractBytes

import javax.jms.BytesMessage; //导入方法依赖的package包/类
/**
 * Extracts the byte array from a JMS message.
 * 
 * @param message  the JMS message, not null
 * @return the extracted byte array, not null
 */
public static byte[] extractBytes(final Message message) {
  if (message instanceof BytesMessage == false) {
    throw new IllegalArgumentException("Message must be an instanceof BytesMessage");
  }
  final BytesMessage bytesMessage = (BytesMessage) message;
  final byte[] bytes;
  try {
    long bodyLength = bytesMessage.getBodyLength();
    if (bodyLength > Integer.MAX_VALUE) {
      throw new IllegalArgumentException("Message too large, maximum size is 2GB, received one of length " + bodyLength);
    }
    bytes = new byte[(int) bodyLength];
    bytesMessage.readBytes(bytes);
  } catch (JMSException jmse) {
    throw new JmsRuntimeException(jmse);
  }
  return bytes;
}
 
开发者ID:DevStreet,项目名称:FinanceAnalytics,代码行数:25,代码来源:JmsByteArrayHelper.java

示例7: getQueue

import javax.jms.BytesMessage; //导入方法依赖的package包/类
private List<String> getQueue() throws Exception {
  List<String> rows = new ArrayList<>();

  Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
  Destination destination = session.createQueue(DESTINATION_NAME);
  MessageConsumer consumer = session.createConsumer(destination);

  Message temp;
  while((temp = consumer.receive(100)) != null) {
    if(temp instanceof BytesMessage) {
      BytesMessage message = (BytesMessage) temp;
      byte[] payload = new byte[(int) message.getBodyLength()];
      message.readBytes(payload);
      rows.add(new String(payload) + RECORD_SEPERATOR);
    } else if(temp instanceof TextMessage) {
      rows.add(((TextMessage) temp).getText());
    } else {
      throw new Exception("Unexpected message type");
    }
  }

  return rows;
}
 
开发者ID:streamsets,项目名称:datacollector,代码行数:24,代码来源:TestJmsTarget.java

示例8: extractString

import javax.jms.BytesMessage; //导入方法依赖的package包/类
@Override
protected String extractString(JMSBindingData binding) throws Exception {
    Message content = binding.getMessage();
    if (content instanceof TextMessage) {
        return TextMessage.class.cast(content).getText();
    
    } else if (content instanceof BytesMessage) {
        BytesMessage sourceBytes = BytesMessage.class.cast(content);
        if (sourceBytes.getBodyLength() > Integer.MAX_VALUE) {
            throw JCAMessages.MESSAGES.theSizeOfMessageContentExceedsBytesThatIsNotSupportedByThisOperationSelector("" + Integer.MAX_VALUE);
        }
        byte[] bytearr = new byte[(int)sourceBytes.getBodyLength()];
        sourceBytes.readBytes(bytearr);
        return new String(bytearr);

    } else if (content instanceof ObjectMessage) {
        ObjectMessage sourceObj = ObjectMessage.class.cast(content);
        return String.class.cast(sourceObj.getObject());

    } else if (content instanceof MapMessage) {
        MapMessage sourceMap = MapMessage.class.cast(content);
        return sourceMap.getString(KEY);
    } else {
        return content.getStringProperty(KEY);
    }
}
 
开发者ID:jboss-switchyard,项目名称:switchyard,代码行数:27,代码来源:JMSOperationSelector.java

示例9: objectFromMsg

import javax.jms.BytesMessage; //导入方法依赖的package包/类
/**
 * Deserialize a JMS message containing a Java serialized abject.
 *
 * @param message JMS input message with a serialized payload
 * @param <T>     Type of the serialized object
 * @return The de-serialized object
 * @throws RuntimeException Can be thrown if deserialization fails.
 */
public static <T extends Serializable> T objectFromMsg(@NotNull final BytesMessage message) {
    requireNonNull(message);

    try {
        byte[] rawData = new byte[(int) message.getBodyLength()];
        message.readBytes(rawData);
        return (T) convertFromBytes(rawData); // NOSONAR
    } catch (javax.jms.JMSException e) {
        throw propagate(e);
    }
}
 
开发者ID:florentw,项目名称:bench,代码行数:20,代码来源:JMSHelper.java

示例10: writeBinaryContentToFile

import javax.jms.BytesMessage; //导入方法依赖的package包/类
/**
     * Write message binary body to provided file or default one in temp directory.
     *
     * @param filePath file to write data to
     * @param message  to be read and written to provided file
     */
    public static void writeBinaryContentToFile(String filePath, Message message, int msgCounter) {
        byte[] readByteArray;
        try {
            File writeBinaryFile;
            if (filePath == null || filePath.equals("")) {
                writeBinaryFile = File.createTempFile("recv_msg_", Long.toString(System.currentTimeMillis()));
            } else {
                writeBinaryFile = new File(filePath + "_" + msgCounter);
            }

            LOG.debug("Write binary content to file '" + writeBinaryFile.getPath() + "'.");
            if (message instanceof BytesMessage) {
                BytesMessage bm = (BytesMessage) message;
                readByteArray = new byte[(int) bm.getBodyLength()];
                bm.reset(); // added to be able to read message content
                bm.readBytes(readByteArray);
                try (FileOutputStream fos = new FileOutputStream(writeBinaryFile)) {
                    fos.write(readByteArray);
                    fos.close();
                }

            } else if (message instanceof StreamMessage) {
                LOG.debug("Writing StreamMessage to");
                StreamMessage sm = (StreamMessage) message;
//        sm.reset(); TODO haven't tested this one
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(sm.readObject());
                oos.close();
            }

        } catch (JMSException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            LOG.error("Error while writing to file '" + filePath + "'.");
            e1.printStackTrace();
        }
    }
 
开发者ID:rh-messaging,项目名称:cli-java,代码行数:45,代码来源:Utils.java

示例11: getPayloadFromBytesMessage

import javax.jms.BytesMessage; //导入方法依赖的package包/类
/**
 * Convert a BytesMessage to a Java Object with the specified type.
 * @param message the input message
 * @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);
    }
}
 
开发者ID:kinglcc,项目名称:spring-boot-jms,代码行数:22,代码来源:Jackson2JmsMessageConverter.java

示例12: saveMessagePayload

import javax.jms.BytesMessage; //导入方法依赖的package包/类
private void saveMessagePayload(Message msg, String baseName) throws JMSException, IOException {
    try (FileOutputStream fos = new FileOutputStream(new File(_messageFileDirectory, baseName + ".payload"))) {
        byte[] payload;
        if (msg instanceof TextMessage) {
            payload = TextMessageData.textToBytes(((TextMessage) msg).getText());
        }
        else if (msg instanceof BytesMessage) {
            BytesMessage bytesMessage = (BytesMessage) msg;
            payload = new byte[(int) bytesMessage.getBodyLength()];
            bytesMessage.readBytes(payload);
        }
        else if (msg instanceof ObjectMessage) {
            payload = _objectMessageAdapter.getObjectPayload((ObjectMessage) msg);
        }
        else if (msg instanceof MapMessage) {
            // Partial support, not all data types are handled and we may not be able to post
            MapMessage mapMessage = (MapMessage) msg;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Properties props = new Properties();
            for (Enumeration<?> mapNames = mapMessage.getMapNames(); mapNames.hasMoreElements();) {
                String mapName = mapNames.nextElement().toString();
                props.setProperty(mapName, mapMessage.getObject(mapName).toString());
            }
            props.store(bos, "Map message properties for " + msg.getJMSMessageID());
            payload = bos.toByteArray();
        }
        else if (msg instanceof StreamMessage) {
            _logger.warn("Can't save payload for {}, stream messages not supported!", msg.getJMSMessageID());
            payload = new byte[0];
        }
        else {
            _logger.warn("Can't save payload for {}, unsupported type {}!", msg.getJMSMessageID(),
                msg.getClass().getName());
            payload = new byte[0];
        }
        fos.write(payload);
        fos.flush();
    }
}
 
开发者ID:erik-wramner,项目名称:JmsTools,代码行数:40,代码来源:DequeueWorker.java

示例13: apply

import javax.jms.BytesMessage; //导入方法依赖的package包/类
@Override
public String apply( BytesMessage t )
{
    if( t != null )
    {
        try
        {
            long l = t.getBodyLength();
            byte b[] = new byte[(int) l];
            t.readBytes( b );
            try( Reader r = new InputStreamReader( new GZIPInputStream( new ByteArrayInputStream( b ) ) ) )
            {
                StringBuilder sb = new StringBuilder();
                char cb[] = new char[1024];
                int s = r.read( cb );
                while( s > -1 )
                {
                    sb.append( cb, 0, s );
                    s = r.read( cb );
                }
                return sb.toString();
            }
        } catch( IOException | JMSException ex )
        {
            Logger.getLogger( getClass().getName() ).log( Level.SEVERE, null, ex );
        }
    }

    return null;
}
 
开发者ID:peter-mount,项目名称:opendata-common,代码行数:31,代码来源:GUnZipBytesMessage.java

示例14: createByteArrayFromBytesMessage

import javax.jms.BytesMessage; //导入方法依赖的package包/类
protected byte[] createByteArrayFromBytesMessage(BytesMessage message) throws JMSException {
    if (message.getBodyLength() > Integer.MAX_VALUE) {
        LOG.warn("Length of BytesMessage is too long: {}", message.getBodyLength());
        return null;
    }
    byte[] result = new byte[(int)message.getBodyLength()];
    message.readBytes(result);
    return result;
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:10,代码来源:JmsBinding.java

示例15: getBody

import javax.jms.BytesMessage; //导入方法依赖的package包/类
private String getBody(BytesMessage bytesMsg) {
    try {
        long length = bytesMsg.getBodyLength();
        byte[] buf = new byte[(int) length];
        bytesMsg.readBytes(buf);
        return new String(buf);
    } catch (JMSException e) {
        String text = "Error getting message body";
        log.error(text, e);
        throw new RuntimeException(text, e);
    }
}
 
开发者ID:espertechinc,项目名称:esper,代码行数:13,代码来源:SampleJMSMessageListener.java


注:本文中的javax.jms.BytesMessage.getBodyLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。