lunes, 2 de septiembre de 2019

Spring Boot mensajeria con JMS (Apache ActiveMQ)

Spring Boot mensajeria con JMS


https://github.com/jalbertomr/springBootJMS/commit/c8774b9eb67e179c4fbd6424a1628d5a089ab667

Estructura del proyecto

Spring proveé los medios para enviar mensajes por medio de cualquier POJO
en este caso se define un simple POJO con dos atributos, (to y body).
email.java
public class Email {

    private String to;
    private String body;

    public Email() {super();}

    public Email(String to, String body) {
        this.to = to;
        this.body = body;
    }

    public String getTo() { return to;}
    public void setTo(String to) {    this.to = to;    }

    public String getBody() {return body;}
    public void setBody(String body) {    this.body = body;    }

    @Override
    public String toString() {
        return "Email [to=" + to + ", body=" + body + "]";
    }
}


Este POJO (email.java) lo recibe una clase Receiver.java.
Receiver.java
@Component
public class Receiver {  //message driven POJO

    @JmsListener(destination="mailbox", containerFactory="miFactory")
    public void receiveMessage(Email email) {
        System.out.println("Recivido <" + email +">");
    }
}

Application.java
@SpringBootApplication
@EnableJms
public class Application {

    @Bean
    public JmsListenerContainerFactory<?> miFactory(ConnectionFactory connectionFactory,
                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // esto provee todos los default a factory, incluyendo el convertidor de mensajes
        configurer.configure(factory, connectionFactory);
        // se pueden sobreescribir los default de ser necesario.
        return factory;
    }

    @Bean  //Serializa el contenido del mensaje a json usando TextMessage
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }
   
    public static void main(String[] args) {
        // corre la app
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
       
        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
       
        //Envia un mensaje con un POJO
        System.out.println("Enviando un mensaje email");
        jmsTemplate.convertAndSend("mailbox", new Email("correo@servidor.com","Contenido del Mensaje"));
    }

}

la ejecucción nos da.
2019-09-02 10:31:26.973  INFO 18340 --- [           main] o.apache.activemq.broker.BrokerService   : Using Persistence Adapter: MemoryPersistenceAdapter
2019-09-02 10:31:26.993  INFO 18340 --- [  JMX connector] o.a.a.broker.jmx.ManagementContext       : JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
2019-09-02 10:31:27.107  INFO 18340 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:bext-VPCF13WFX-32979-1567438286980-0:1) is starting
2019-09-02 10:31:27.111  INFO 18340 --- [           main] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:bext-VPCF13WFX-32979-1567438286980-0:1) started
2019-09-02 10:31:27.111  INFO 18340 --- [           main] o.apache.activemq.broker.BrokerService   : For help or more information please see: http://activemq.apache.org
2019-09-02 10:31:27.140  INFO 18340 --- [           main] o.a.activemq.broker.TransportConnector   : Connector vm://localhost started
2019-09-02 10:31:27.204  INFO 18340 --- [           main] com.bext.Application                     : Started Application in 1.655 seconds (JVM running for 2.445)
Enviando un mensaje email
Recivido <Email [to=correo@servidor.com, body=Contenido del Mensaje]>
2019-09-02 10:31:35.206  INFO 18340 --- [on(4)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
2019-09-02 10:31:35.370  INFO 18340 --- [on(4)-127.0.0.1] o.a.activemq.broker.TransportConnector   : Connector vm://localhost stopped
2019-09-02 10:31:35.371  INFO 18340 --- [on(4)-127.0.0.1] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:bext-VPCF13WFX-32979-1567438286980-0:1) is shutting down
2019-09-02 10:31:35.380  INFO 18340 --- [on(4)-127.0.0.1] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:bext-VPCF13WFX-32979-1567438286980-0:1) uptime 8.512 seconds
2019-09-02 10:31:35.380  INFO 18340 --- [on(4)-127.0.0.1] o.apache.activemq.broker.BrokerService   : Apache ActiveMQ 5.15.9 (localhost, ID:bext-VPCF13WFX-32979-1567438286980-0:1) is shutdown




eot

No hay comentarios:

Publicar un comentario