Search in sources :

Example 1 with MapMessage

use of jakarta.jms.MapMessage in project spring-framework by spring-projects.

the class SimpleMessageConverter method createMessageForMap.

/**
 * Create a JMS MapMessage for the given Map.
 * @param map the Map to convert
 * @param session current JMS session
 * @return the resulting message
 * @throws JMSException if thrown by JMS methods
 * @see jakarta.jms.Session#createMapMessage
 */
protected MapMessage createMessageForMap(Map<?, ?> map, Session session) throws JMSException {
    MapMessage message = session.createMapMessage();
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        Object key = entry.getKey();
        if (!(key instanceof String)) {
            throw new MessageConversionException("Cannot convert non-String key of type [" + ObjectUtils.nullSafeClassName(key) + "] to JMS MapMessage entry");
        }
        message.setObject((String) key, entry.getValue());
    }
    return message;
}
Also used : MapMessage(jakarta.jms.MapMessage) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with MapMessage

use of jakarta.jms.MapMessage in project spring-framework by spring-projects.

the class SimpleMessageConverterTests method testMapConversionWhereMapHasNonStringTypesForKeys.

@Test
public void testMapConversionWhereMapHasNonStringTypesForKeys() throws JMSException {
    MapMessage message = mock(MapMessage.class);
    Session session = mock(Session.class);
    given(session.createMapMessage()).willReturn(message);
    Map<Integer, String> content = new HashMap<>(1);
    content.put(1, "value1");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() -> converter.toMessage(content, session));
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) HashMap(java.util.HashMap) MapMessage(jakarta.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 3 with MapMessage

use of jakarta.jms.MapMessage in project spring-framework by spring-projects.

the class SimpleMessageConverterTests method testMapConversionWhereMapHasNNullForKey.

@Test
public void testMapConversionWhereMapHasNNullForKey() throws JMSException {
    MapMessage message = mock(MapMessage.class);
    Session session = mock(Session.class);
    given(session.createMapMessage()).willReturn(message);
    Map<Object, String> content = new HashMap<>(1);
    content.put(null, "value1");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() -> converter.toMessage(content, session));
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) HashMap(java.util.HashMap) MapMessage(jakarta.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 4 with MapMessage

use of jakarta.jms.MapMessage in project spring-framework by spring-projects.

the class SimpleMessageConverterTests method testMapConversion.

@Test
public void testMapConversion() throws JMSException {
    Session session = mock(Session.class);
    MapMessage message = mock(MapMessage.class);
    Map<String, String> content = new HashMap<>(2);
    content.put("key1", "value1");
    content.put("key2", "value2");
    given(session.createMapMessage()).willReturn(message);
    given(message.getMapNames()).willReturn(Collections.enumeration(content.keySet()));
    given(message.getObject("key1")).willReturn("value1");
    given(message.getObject("key2")).willReturn("value2");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    Message msg = converter.toMessage(content, session);
    assertThat(converter.fromMessage(msg)).isEqualTo(content);
    verify(message).setObject("key1", "value1");
    verify(message).setObject("key2", "value2");
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) MapMessage(jakarta.jms.MapMessage) ObjectMessage(jakarta.jms.ObjectMessage) HashMap(java.util.HashMap) MapMessage(jakarta.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Aggregations

MapMessage (jakarta.jms.MapMessage)4 HashMap (java.util.HashMap)4 Session (jakarta.jms.Session)3 Test (org.junit.jupiter.api.Test)3 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)3 MessageConversionException (org.springframework.jms.support.converter.MessageConversionException)2 BytesMessage (jakarta.jms.BytesMessage)1 Message (jakarta.jms.Message)1 ObjectMessage (jakarta.jms.ObjectMessage)1 TextMessage (jakarta.jms.TextMessage)1 Map (java.util.Map)1