Skip to content
Snippets Groups Projects

JMS Producer Example

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by German Adolfo Faller Farias

    ejemplo de productor de mensajes JMS

    producer.java 1.83 KiB
    package uy.com.faller.mensajeria.envio.impl;
    
    import org.apache.log4j.Logger;
    
    import javax.annotation.Resource;
    import javax.ejb.EJB;
    import javax.ejb.Stateless;
    import javax.jms.*;
    import java.io.Serializable;
    
    @Stateless(name = "EJBSendBean")
    public class EJBSendBean  {
        private static final Logger LOG = Logger.getLogger(EJBSendBean.class);
        private static final String COLA_DEST = "java:/queue/msj_queue";
    
        @Resource(lookup = "java:/ConnectionFactory") private ConnectionFactory connectionFactory;
        @Resource(lookup = COLA_DEST) private Queue sendQueue;
        
        public void sendMessage(Serializable mensajeObject, String uuidSolicitud) throws Exception {
            try {
                Session session = null;
                MessageProducer producer;
                ObjectMessage objMessage;
                Connection connection = null;
    
                try {
                    connection = connectionFactory.createConnection();
                    connection.start();
    
                    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                    producer = session.createProducer(sendQueue);
                    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    
                    // Se setea la prioridad en la cola cuando son sincrónicos
                    producer.setPriority(1);
    
                    objMessage = session.createObjectMessage(mensajeObject);
                    objMessage.setStringProperty(PropiedadesConstants.UUID, uuidSolicitud);
                    
                    producer.send(objMessage);
                } finally {
                    if (session != null) {
                        session.close();
                    }
                    if (connection != null) {
                        connection.close();
                    }
                }
            } catch (JMSException | AuditoriaException e) {
                throw new Exception(Codigos.ERROR, e);
            }
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment