Skip to content

Commit 482ce3b

Browse files
author
lawalk
committed
Refactoring code with adding producer class
1 parent 9d78377 commit 482ce3b

File tree

6 files changed

+79
-39
lines changed

6 files changed

+79
-39
lines changed
Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
package com.javatodev.api.configuration;
22

33
import jakarta.jms.ConnectionFactory;
4+
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
67
import org.springframework.jms.annotation.EnableJms;
78
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
9+
import org.springframework.jms.config.JmsListenerContainerFactory;
10+
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
11+
import org.springframework.jms.support.converter.MessageConverter;
12+
import org.springframework.jms.support.converter.MessageType;
813

914
@Configuration
1015
@EnableJms
1116
public class JmsConfiguration {
1217

1318
/**
14-
* Set the connection with the ActiveMQ
19+
* Create JMS Connection Factory
1520
* @param connectionFactory
16-
* @return JmsListenerContainerFactory
21+
* @param configurer
22+
* @return Connection Factory
1723
*/
1824
@Bean
19-
public DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory(ConnectionFactory connectionFactory){
20-
DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
21-
defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory);
22-
// Set concurrency with minimum 5 consumers and automatically scale up to maximum of 10 consumers
23-
defaultJmsListenerContainerFactory.setConcurrency("5-10");
24-
return defaultJmsListenerContainerFactory;
25+
public JmsListenerContainerFactory<?> jmsFactory(ConnectionFactory connectionFactory,
26+
DefaultJmsListenerContainerFactoryConfigurer configurer) {
27+
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
28+
factory.setMessageConverter(jacksonJmsMessageConverter());
29+
configurer.configure(factory, connectionFactory);
30+
return factory;
2531
}
32+
33+
/**
34+
* Serialize message content to json using TextMessage
35+
*
36+
* @return Message Converter
37+
*/
38+
@Bean
39+
public MessageConverter jacksonJmsMessageConverter() {
40+
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
41+
converter.setTargetType(MessageType.TEXT);
42+
converter.setTypeIdPropertyName("_asb_");
43+
return converter;
44+
}
45+
2646
}

spring-boot-api-with-activemq/src/main/java/com/javatodev/api/consumer/StoreMessageConsumer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class StoreMessageConsumer {
1313
* Message Listener of ActiveMQ queue
1414
* @param store
1515
*/
16-
@JmsListener(destination = "store_msg_queue")
17-
public void messageListener(Store store){
18-
log.info("Message Received. {}",store);
16+
@JmsListener(destination = "${activemq.destination}", containerFactory = "jmsFactory")
17+
public void processToDo(Store store) {
18+
log.info("Consumer> " + store);
1919
}
2020
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.javatodev.api.controller;
22

33
import com.javatodev.api.dto.Store;
4+
import com.javatodev.api.producer.StoreMessageProducer;
45
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.http.HttpStatus;
6-
import org.springframework.http.ResponseEntity;
6+
import org.springframework.beans.factory.annotation.Value;
77
import org.springframework.jms.core.JmsTemplate;
88
import org.springframework.web.bind.annotation.PostMapping;
99
import org.springframework.web.bind.annotation.RequestBody;
@@ -17,18 +17,20 @@ public class StoreMessageController {
1717
@Autowired
1818
private JmsTemplate jmsTemplate;
1919

20+
@Autowired
21+
StoreMessageProducer storeMessageProducer;
22+
23+
@Value("${activemq.destination}")
24+
private String destination;
25+
2026
/**
2127
* API for publish message for ActiveMQ queue
2228
* @param store
23-
* @return ResponseEntity
29+
* @return String
2430
*/
2531
@PostMapping("/publish")
26-
public ResponseEntity<String> publishMessage(@RequestBody Store store){
27-
try {
28-
jmsTemplate.convertAndSend("store_msg_queue", store);
29-
return new ResponseEntity<>("Request Sent", HttpStatus.OK);
30-
}catch (Exception e){
31-
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
32-
}
32+
public String publishMessage(@RequestBody Store store){
33+
storeMessageProducer.sendTo(destination,store);
34+
return "Success";
3335
}
3436
}
Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
package com.javatodev.api.dto;
22

3-
import lombok.Data;
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
46

5-
import java.io.Serializable;
6-
7-
@Data
8-
public class Store implements Serializable {
9-
10-
private static final long serialVersionUID = 1L;
7+
@Getter
8+
@Setter
9+
@ToString
10+
public class Store{
1111

1212
private int itemId;
1313
private String itemName;
1414
private int itemQuantity;
15-
16-
@Override
17-
public String toString(){
18-
return "Store{" +
19-
"item id ='"+ itemId +'\'' +
20-
", item Name ='"+ itemName + '\'' +
21-
", item Quantity ='"+ itemQuantity + '\'' +
22-
"}";
23-
24-
}
2515
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javatodev.api.producer;
2+
3+
import com.javatodev.api.dto.Store;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jms.core.JmsTemplate;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Slf4j
11+
public class StoreMessageProducer {
12+
13+
@Autowired
14+
private JmsTemplate jmsTemplate;
15+
16+
/**
17+
* Convert and publish the message to the queue
18+
*
19+
* @param destination
20+
* @param store
21+
*/
22+
public void sendTo(String destination, Store store) {
23+
jmsTemplate.convertAndSend(destination, store);
24+
log.info("Producer> Message Sent");
25+
}
26+
27+
}

spring-boot-api-with-activemq/src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ server.port = 8089
33
spring.activemq.broker-url=tcp://localhost:61616
44
spring.activemq.user=admin
55
spring.activemq.password=admin
6-
spring.activemq.packages.trust-all=true
6+
7+
activemq.destination=store

0 commit comments

Comments
 (0)