Şu satırı dahil ederiz
import org.springframework.jms.annotation.JmsListenerConfigurer;
Açıklaması şöyle. Yani @JmsListener anotasyonunu kullanmak daha kolay :)
Optional interface to be implemented by a Spring managed bean willing to customize how JMS listener endpoints are configured. Typically used to define the default JmsListenerContainerFactory to use or for registering JMS endpoints in a programmatic fashion as opposed to the declarative approach of using the @JmsListener annotation.
configureJmsListeners metodu
ÖrnekElimizde şöyle bir kod olsun
@Componentpublic class QueueService implements MessageListener {@Autowiredprivate JmsTemplate jmsTemplate;public void send(String destination, String message) {jmsTemplate.convertAndSend(destination, message);}@Overridepublic void onMessage(Message message) {if (message instanceof ActiveMQTextMessage) {ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message;try {LOGGER.info("Completed task " + textMessage.getText());} catch (InterruptedException | JMSException e) {e.printStackTrace();}} else {LOGGER.error("Message is not a text message " + message.toString());}}}
Şöyle yaparız
@SpringBootApplication@EnableJmspublic class SpringBootApplication implements JmsListenerConfigurer {@Autowiredprivate QueueService queueService;public static void main(String[] args) {SpringApplication.run(SpringBootApplication.class, args);}@Overridepublic void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();endpoint.setId("myId");endpoint.setDestination("queueName");endpoint.setMessageListener(queueService);registrar.registerEndpoint(endpoint);}}
Örnek
Şöyle yaparız
@SpringBootApplication@EnableJmspublic class SpringBootApplication implements JmsListenerConfigurer {@Autowiredprivate QueueService queueService;public static void main(String[] args) {SpringApplication.run(SpringBootApplication.class, args);}@Overridepublic void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();endpoint.setId("myId");endpoint.setDestination("queueName");endpoint.setMessageListener(queueService);registrar.registerEndpoint(endpoint);}}
Hiç yorum yok:
Yorum Gönder