27 Aralık 2020 Pazar

SpringJMS JmsListenerConfigurer Arayüzü

Giriş
Ş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
Örnek
Elimizde şöyle bir kod olsun
@Component
public class QueueService implements MessageListener {
  @Autowired
  private JmsTemplate jmsTemplate;
  public void send(String destination, String message) {
    jmsTemplate.convertAndSend(destination, message);
  }
  @Override
  public 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
@EnableJms
public class SpringBootApplication implements JmsListenerConfigurer {
  @Autowired
  private QueueService queueService;
  
  public static void main(String[] args) {
    SpringApplication.run(SpringBootApplication.class, args);
  }
  
  @Override
  public 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
@EnableJms
public class SpringBootApplication implements JmsListenerConfigurer {
  @Autowired
  private QueueService queueService;
  public static void main(String[] args) {
    SpringApplication.run(SpringBootApplication.class, args);
  }

  @Override
  public 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