Reputation: 13
I am new to Spring Boot and coding for my college project work and I need some help. I want to create a spring boot application, which starts a factory class that will create and starts 10 synchronous clients. These clients must be listening constantly to the queues. Below is my code. I am not sure, if I am on right track and need some help. Is my implementation correct? How can I make sure that 10 synchronous clients are created? How do I use identifier to identify which message client processed the message ?
Application.java
@SpringBootApplication
public class Application {
@Bean
public MesFactory mesFactory(){
return new MesFactory();
}
public static void main(String... args) {
SpringApplication.run(Application.class, args);
}
}
public class MesFactory {
private ExecutorService executorService =
Executors.newFixedThreadPool(10);
@PostConstruct
public void build(){
executorService.execute(() -> new MesClient());
}
}
@Component
@EnableJms
public class MesClient {
private static final Log log = LogFactory.getLog(MesClient.class);
@Autowired
private JmsTemplate jmsTemplate;
@JmsListener(destination = "incoming-messages-queue")
public void receive(String message) {
System.out.println("Received <" + message + ">");
doPerformMessage(message);
}
public void doPerformMessage(String message){
// parsing happens here
}
@Bean
ConnectionFactory connectionFactory() {
return new RMQConnectionFactory();
}
@Scheduled(fixedRate = 100000000L)
public void sendMessage() {
String message= "Hi World";
// Coerce a javax.jms.MessageCreator
MessageCreator messageCreator = (Session session) -> {
return session.createTextMessage(message);
};
// And publish to RabbitMQ using Spring's JmsTemplate
jmsTemplate.send("incoming-messages-queue", messageCreator);
}
}
Upvotes: 1
Views: 2436
Reputation: 174729
Set the boot properties...
spring.jms.listener.concurrency=10
spring.jms.listener.max-concurrency=10
The listener container will start 10 threads - each consuming from the same queue; they will invoke the JMS listener method.
See the Spring Boot documentation - Application Properties and scroll down to the JMS section.
Upvotes: 3