下面列出了怎么用javax.jms.MessageFormatRuntimeException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testJMSContextConsumerThrowsMessageFormatExceptionOnMalformedBody() throws Exception {
Queue queue = createQueue(true, "ContextMalformedBodyTestQueue");
JMSContext context = qraConnectionFactory.createContext();
JMSProducer producer = context.createProducer();
TextMessage message = context.createTextMessage("TestMessage");
producer.send(queue, message);
JMSConsumer consumer = context.createConsumer(queue);
try {
consumer.receiveBody(Boolean.class);
fail("Should thrown MessageFormatException");
} catch (MessageFormatRuntimeException mfre) {
// Do nothing test passed
} catch (Exception e) {
fail("Threw wrong exception, should be MessageFormatRuntimeException, instead got: " + e.getClass().getCanonicalName());
}
}
@Test
public void testSetters() {
long v = random.nextLong();
producer.setDeliveryDelay(v);
Assert.assertEquals(v, producer.getDeliveryDelay());
long l = random.nextLong();
producer.setTimeToLive(l);
Assert.assertEquals(l, producer.getTimeToLive());
String id = "ID: jms2-tests-correlation-id" + random.nextLong();
producer.setJMSCorrelationID(id);
Assert.assertEquals(id, producer.getJMSCorrelationID());
//set a property of an invalid type (ArrayList)
try {
producer.setProperty("name1", new ArrayList<String>(2));
fail("didn't get expected MessageFormatRuntimeException");
} catch (MessageFormatRuntimeException e) {
//expected.
}
}
@Test
public void testSetPropertyConversions() {
JMSProducer producer = context.createProducer();
producer.setProperty(STRING_PROPERTY_NAME, STRING_PROPERTY_VALUE);
producer.setProperty(BYTE_PROPERTY_NAME, Byte.valueOf(BYTE_PROPERTY_VALUE));
producer.setProperty(BOOLEAN_PROPERTY_NAME, Boolean.valueOf(BOOLEAN_PROPERTY_VALUE));
producer.setProperty(SHORT_PROPERTY_NAME, Short.valueOf(SHORT_PROPERTY_VALUE));
producer.setProperty(INTEGER_PROPERTY_NAME, Integer.valueOf(INTEGER_PROPERTY_VALUE));
producer.setProperty(LONG_PROPERTY_NAME, Long.valueOf(LONG_PROPERTY_VALUE));
producer.setProperty(FLOAT_PROPERTY_NAME, Float.valueOf(FLOAT_PROPERTY_VALUE));
producer.setProperty(DOUBLE_PROPERTY_NAME, Double.valueOf(DOUBLE_PROPERTY_VALUE));
try {
producer.setProperty(STRING_PROPERTY_NAME, UUID.randomUUID());
fail("Should not be able to set non-primitive type");
} catch (MessageFormatRuntimeException mfe) {
}
assertNull(producer.getObjectProperty("Unknown"));
assertEquals(STRING_PROPERTY_VALUE, producer.getStringProperty(STRING_PROPERTY_NAME));
assertEquals(BYTE_PROPERTY_VALUE, producer.getByteProperty(BYTE_PROPERTY_NAME));
assertEquals(BOOLEAN_PROPERTY_VALUE, producer.getBooleanProperty(BOOLEAN_PROPERTY_NAME));
assertEquals(SHORT_PROPERTY_VALUE, producer.getShortProperty(SHORT_PROPERTY_NAME));
assertEquals(INTEGER_PROPERTY_VALUE, producer.getIntProperty(INTEGER_PROPERTY_NAME));
assertEquals(LONG_PROPERTY_VALUE, producer.getLongProperty(LONG_PROPERTY_NAME));
assertEquals(FLOAT_PROPERTY_VALUE, producer.getFloatProperty(FLOAT_PROPERTY_NAME), 0.0);
assertEquals(DOUBLE_PROPERTY_VALUE, producer.getDoubleProperty(DOUBLE_PROPERTY_NAME), 0.0);
assertEquals(STRING_PROPERTY_VALUE, producer.getObjectProperty(STRING_PROPERTY_NAME));
assertEquals(BYTE_PROPERTY_VALUE, producer.getObjectProperty(BYTE_PROPERTY_NAME));
assertEquals(BOOLEAN_PROPERTY_VALUE, producer.getObjectProperty(BOOLEAN_PROPERTY_NAME));
assertEquals(SHORT_PROPERTY_VALUE, producer.getObjectProperty(SHORT_PROPERTY_NAME));
assertEquals(INTEGER_PROPERTY_VALUE, producer.getObjectProperty(INTEGER_PROPERTY_NAME));
assertEquals(LONG_PROPERTY_VALUE, producer.getObjectProperty(LONG_PROPERTY_NAME));
assertEquals(FLOAT_PROPERTY_VALUE, producer.getObjectProperty(FLOAT_PROPERTY_NAME));
assertEquals(DOUBLE_PROPERTY_VALUE, producer.getObjectProperty(DOUBLE_PROPERTY_NAME));
}
@Test
public void testSetObjectPropertyWithInvalidObject() {
JMSProducer producer = context.createProducer();
try {
producer.setProperty(GOOD_PROPERTY_NAME, UUID.randomUUID());
fail("Should not accept invalid property name");
} catch (MessageFormatRuntimeException mfre) {}
}
@Test
public void testSendNullMessageThrowsMFRE() throws JMSException {
JMSProducer producer = context.createProducer();
try {
producer.send(JMS_DESTINATION, (Message) null);
fail("Should throw a MessageFormatRuntimeException");
} catch (MessageFormatRuntimeException mfre) {
} catch (Exception e) {
fail("Should throw a MessageFormatRuntimeException");
}
}
@Test
public void testInvalidMessage() {
JMSProducer producer = context.createProducer();
try {
producer.send(queue1, (Message) null);
Assert.fail("null msg");
} catch (MessageFormatRuntimeException expected) {
// no-op
}
}
@Override
public JMSProducer send(Destination destination, Message message) {
if (message == null) {
throw new MessageFormatRuntimeException("null message");
}
try {
if (jmsHeaderCorrelationID != null) {
message.setJMSCorrelationID(jmsHeaderCorrelationID);
}
if (jmsHeaderCorrelationIDAsBytes != null && jmsHeaderCorrelationIDAsBytes.length > 0) {
message.setJMSCorrelationIDAsBytes(jmsHeaderCorrelationIDAsBytes);
}
if (jmsHeaderReplyTo != null) {
message.setJMSReplyTo(jmsHeaderReplyTo);
}
if (jmsHeaderType != null) {
message.setJMSType(jmsHeaderType);
}
// XXX HORNETQ-1209 "JMS 2.0" can this be a foreign msg?
// if so, then "SimpleString" properties will trigger an error.
setProperties(message);
if (completionListener != null) {
CompletionListener wrapped = new CompletionListenerWrapper(completionListener);
producer.send(destination, message, wrapped);
} else {
producer.send(destination, message);
}
} catch (JMSException e) {
throw JmsExceptionUtils.convertToRuntimeException(e);
}
return this;
}
@Override
public JMSProducer send(Destination destination, Map<String, Object> body) {
MapMessage message = context.createMapMessage();
if (body != null) {
try {
for (Entry<String, Object> entry : body.entrySet()) {
final String name = entry.getKey();
final Object v = entry.getValue();
if (v instanceof String) {
message.setString(name, (String) v);
} else if (v instanceof Long) {
message.setLong(name, (Long) v);
} else if (v instanceof Double) {
message.setDouble(name, (Double) v);
} else if (v instanceof Integer) {
message.setInt(name, (Integer) v);
} else if (v instanceof Character) {
message.setChar(name, (Character) v);
} else if (v instanceof Short) {
message.setShort(name, (Short) v);
} else if (v instanceof Boolean) {
message.setBoolean(name, (Boolean) v);
} else if (v instanceof Float) {
message.setFloat(name, (Float) v);
} else if (v instanceof Byte) {
message.setByte(name, (Byte) v);
} else if (v instanceof byte[]) {
byte[] array = (byte[]) v;
message.setBytes(name, array, 0, array.length);
} else {
message.setObject(name, v);
}
}
} catch (JMSException e) {
throw new MessageFormatRuntimeException(e.getMessage());
}
}
send(destination, message);
return this;
}
@Override
public JMSProducer send(Destination destination, byte[] body) {
BytesMessage message = context.createBytesMessage();
if (body != null) {
try {
message.writeBytes(body);
} catch (JMSException e) {
throw new MessageFormatRuntimeException(e.getMessage());
}
}
send(destination, message);
return this;
}
@Override
public JMSProducer setProperty(String name, Object value) {
checkName(name);
try {
TypedProperties.setObjectProperty(new SimpleString(name), value, properties);
} catch (ActiveMQPropertyConversionException amqe) {
throw new MessageFormatRuntimeException(amqe.getMessage());
} catch (RuntimeException e) {
throw new JMSRuntimeException(e.getMessage());
}
return this;
}
@Override
public boolean getBooleanProperty(String name) {
try {
return properties.getBooleanProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
} catch (RuntimeException e) {
throw new JMSRuntimeException(e.getMessage());
}
}
@Override
public byte getByteProperty(String name) {
try {
return properties.getByteProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
}
}
@Override
public short getShortProperty(String name) {
try {
return properties.getShortProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
}
}
@Override
public int getIntProperty(String name) {
try {
return properties.getIntProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
}
}
@Override
public long getLongProperty(String name) {
try {
return properties.getLongProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
}
}
@Override
public float getFloatProperty(String name) {
try {
return properties.getFloatProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
}
}
@Override
public double getDoubleProperty(String name) {
try {
return properties.getDoubleProperty(new SimpleString(name));
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
}
}
@Override
public String getStringProperty(String name) {
try {
SimpleString prop = properties.getSimpleStringProperty(new SimpleString(name));
if (prop == null)
return null;
return prop.toString();
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
} catch (RuntimeException e) {
throw new JMSRuntimeException(e.getMessage());
}
}
@Override
public Object getObjectProperty(String name) {
try {
SimpleString key = new SimpleString(name);
Object property = properties.getProperty(key);
if (stringPropertyNames.contains(key)) {
property = property.toString();
}
return property;
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
} catch (RuntimeException e) {
throw new JMSRuntimeException(e.getMessage());
}
}
@Override
public Set<String> getPropertyNames() {
try {
return properties.getMapNames();
} catch (ActiveMQPropertyConversionException ce) {
throw new MessageFormatRuntimeException(ce.getMessage());
} catch (RuntimeException e) {
throw new JMSRuntimeException(e.getMessage());
}
}
/**
* Converts instances of sub-classes of {@link JMSException} into the corresponding sub-class of
* {@link JMSRuntimeException}.
*
* @param e
* @return
*/
public static JMSRuntimeException convertToRuntimeException(JMSException e) {
if (e instanceof javax.jms.IllegalStateException) {
return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidClientIDException) {
return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidDestinationException) {
return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidSelectorException) {
return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof JMSSecurityException) {
return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageFormatException) {
return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageNotWriteableException) {
return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof ResourceAllocationException) {
return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionInProgressException) {
return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionRolledBackException) {
return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
@Test
public void testSetPropertyConversions() {
JMSProducer producer = context.createProducer();
producer.setProperty(STRING_PROPERTY_NAME, STRING_PROPERTY_VALUE);
producer.setProperty(BYTE_PROPERTY_NAME, Byte.valueOf(BYTE_PROPERTY_VALUE));
producer.setProperty(BOOLEAN_PROPERTY_NAME, Boolean.valueOf(BOOLEAN_PROPERTY_VALUE));
producer.setProperty(SHORT_PROPERTY_NAME, Short.valueOf(SHORT_PROPERTY_VALUE));
producer.setProperty(INTEGER_PROPERTY_NAME, Integer.valueOf(INTEGER_PROPERTY_VALUE));
producer.setProperty(LONG_PROPERTY_NAME, Long.valueOf(LONG_PROPERTY_VALUE));
producer.setProperty(FLOAT_PROPERTY_NAME, Float.valueOf(FLOAT_PROPERTY_VALUE));
producer.setProperty(DOUBLE_PROPERTY_NAME, Double.valueOf(DOUBLE_PROPERTY_VALUE));
try {
producer.setProperty(STRING_PROPERTY_NAME, UUID.randomUUID());
fail("Should not be able to set non-primitive type");
} catch (MessageFormatRuntimeException mfe) {
}
assertNull(producer.getObjectProperty("Unknown"));
assertEquals(STRING_PROPERTY_VALUE, producer.getStringProperty(STRING_PROPERTY_NAME));
assertEquals(BYTE_PROPERTY_VALUE, producer.getByteProperty(BYTE_PROPERTY_NAME));
assertEquals(BOOLEAN_PROPERTY_VALUE, producer.getBooleanProperty(BOOLEAN_PROPERTY_NAME));
assertEquals(SHORT_PROPERTY_VALUE, producer.getShortProperty(SHORT_PROPERTY_NAME));
assertEquals(INTEGER_PROPERTY_VALUE, producer.getIntProperty(INTEGER_PROPERTY_NAME));
assertEquals(LONG_PROPERTY_VALUE, producer.getLongProperty(LONG_PROPERTY_NAME));
assertEquals(FLOAT_PROPERTY_VALUE, producer.getFloatProperty(FLOAT_PROPERTY_NAME), 0.0);
assertEquals(DOUBLE_PROPERTY_VALUE, producer.getDoubleProperty(DOUBLE_PROPERTY_NAME), 0.0);
assertEquals(STRING_PROPERTY_VALUE, producer.getObjectProperty(STRING_PROPERTY_NAME));
assertEquals(BYTE_PROPERTY_VALUE, producer.getObjectProperty(BYTE_PROPERTY_NAME));
assertEquals(BOOLEAN_PROPERTY_VALUE, producer.getObjectProperty(BOOLEAN_PROPERTY_NAME));
assertEquals(SHORT_PROPERTY_VALUE, producer.getObjectProperty(SHORT_PROPERTY_NAME));
assertEquals(INTEGER_PROPERTY_VALUE, producer.getObjectProperty(INTEGER_PROPERTY_NAME));
assertEquals(LONG_PROPERTY_VALUE, producer.getObjectProperty(LONG_PROPERTY_NAME));
assertEquals(FLOAT_PROPERTY_VALUE, producer.getObjectProperty(FLOAT_PROPERTY_NAME));
assertEquals(DOUBLE_PROPERTY_VALUE, producer.getObjectProperty(DOUBLE_PROPERTY_NAME));
}
@Test
public void testSetObjectPropetryWithInvalidObject() {
JMSProducer producer = context.createProducer();
try {
producer.setProperty(GOOD_PROPERTY_NAME, UUID.randomUUID());
fail("Should not accept invalid property name");
} catch (MessageFormatRuntimeException mfre) {}
}
@Test
public void testSendNullMessageThrowsMFRE() throws JMSException {
JmsSession session = Mockito.mock(JmsSession.class);
JmsMessageProducer messageProducer = Mockito.mock(JmsMessageProducer.class);
JmsProducer producer = new JmsProducer(session, messageProducer);
try {
producer.send(JMS_DESTINATION, (Message) null);
fail("Should throw a MessageFormatRuntimeException");
} catch (MessageFormatRuntimeException mfre) {
} catch (Exception e) {
fail("Should throw a MessageFormatRuntimeException");
}
}
public void doTestReceiveBodyFailsDoesNotAcceptMessage(int sessionMode) throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
final String content = "Message-Content";
Queue queue = context.createQueue("myQueue");
DescribedType amqpValueContent = new AmqpValueDescribedType(content);
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent);
testPeer.expectEnd();
testPeer.expectClose();
JMSConsumer messageConsumer = context.createConsumer(queue);
try {
messageConsumer.receiveBody(Boolean.class, 3000);
fail("Should not read as Boolean type");
} catch (MessageFormatRuntimeException mfre) {
}
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}
public static JMSRuntimeException toRuntimeException(final JMSException e) {
if (e instanceof javax.jms.IllegalStateException) {
return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidClientIDException) {
return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidDestinationException) {
return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof InvalidSelectorException) {
return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof JMSSecurityException) {
return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageFormatException) {
return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof MessageNotWriteableException) {
return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof ResourceAllocationException) {
return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionInProgressException) {
return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
if (e instanceof TransactionRolledBackException) {
return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e);
}
private <T> T getProperty(final String key, final Class<T> type) {
final Object val = properties.get(key);
if (val == null || type.isInstance(val)) {
return type.cast(val);
}
try {
return type.cast(propertyEditorRegistry.getValue(type, val.toString()));
} catch (final PropertyEditorException pee) {
throw new MessageFormatRuntimeException(pee.getMessage());
}
}
@Override
public JMSProducer send(final Destination destination, final Message message) {
if (message == null) {
throw new MessageFormatRuntimeException("null message");
}
try {
if (jmsHeaderCorrelationID != null) {
message.setJMSCorrelationID(jmsHeaderCorrelationID);
}
if (jmsHeaderCorrelationIDAsBytes != null && jmsHeaderCorrelationIDAsBytes.length > 0) {
message.setJMSCorrelationIDAsBytes(jmsHeaderCorrelationIDAsBytes);
}
if (jmsHeaderReplyTo != null) {
message.setJMSReplyTo(jmsHeaderReplyTo);
}
if (jmsHeaderType != null) {
message.setJMSType(jmsHeaderType);
}
setProperties(message);
if (completionListener != null) {
producer.send(destination, message, completionListener);
} else {
producer.send(destination, message);
}
} catch (final JMSException e) {
throw toRuntimeException(e);
}
return this;
}
@Override
public JMSProducer send(final Destination destination, final Map<String, Object> body) {
final MapMessage message = wrap(context.createMapMessage());
if (body != null) {
try {
for (final Map.Entry<String, Object> entry : body.entrySet()) {
final String name = entry.getKey();
final Object v = entry.getValue();
if (v instanceof String) {
message.setString(name, (String) v);
} else if (v instanceof Long) {
message.setLong(name, (Long) v);
} else if (v instanceof Double) {
message.setDouble(name, (Double) v);
} else if (v instanceof Integer) {
message.setInt(name, (Integer) v);
} else if (v instanceof Character) {
message.setChar(name, (Character) v);
} else if (v instanceof Short) {
message.setShort(name, (Short) v);
} else if (v instanceof Boolean) {
message.setBoolean(name, (Boolean) v);
} else if (v instanceof Float) {
message.setFloat(name, (Float) v);
} else if (v instanceof Byte) {
message.setByte(name, (Byte) v);
} else if (v instanceof byte[]) {
byte[] array = (byte[]) v;
message.setBytes(name, array, 0, array.length);
} else {
message.setObject(name, v);
}
}
} catch (final JMSException e) {
throw new MessageFormatRuntimeException(e.getMessage());
}
}
send(destination, message);
return this;
}
@Override
public JMSProducer send(final Destination destination, final byte[] body) {
final BytesMessage message = wrap(context.createBytesMessage());
if (body != null) {
try {
message.writeBytes(body);
} catch (final JMSException e) {
throw new MessageFormatRuntimeException(e.getMessage());
}
}
send(destination, message);
return this;
}