https://docs.spring.io/spring-framework/docs/3.0.x/spring-framework-reference/html/jms.html

Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
@EnableJms
public class JmsConfig {


@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}

@Bean
public Queue queue() {
return new ActiveMQQueue("amq");
}
}

Producer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// msg entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Email {
private String to;
private String body;
}

@Component
@RequiredArgsConstructor
public class MsgSender {
private final JmsMessagingTemplate jmsMessagingTemplate;
private final Queue queue;

public void send() {
jmsMessagingTemplate.convertAndSend(queue, new Email("info@example.com", "Hello"));
}
}

Consumer

1
2
3
4
5
6
7
8
9
@Component
@Slf4j
public class MsgReceiver {

@JmsListener(destination = "amq")
public void receive(Email email) {
log.info("Received < {} >", email);
}
}