Untitled

mail@pastecode.io avatar
unknown
plain_text
10 months ago
1.7 kB
2
Indexable
Never
package bank.byblos.test.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.stereotype.Component;

@Component
public class Producer2 {

  private Connection connection;
  private Session session;
  private MessageProducer producer;

  public void openConnection() throws JMSException {
    // Create a new connection factory
    ConnectionFactory connectionFactory =
        new ActiveMQConnectionFactory(
            ActiveMQConnection.DEFAULT_BROKER_URL);
    connection = connectionFactory.createConnection();
  }

  public void closeConnection() throws JMSException {
    connection.close();
  }

  public void createProducer(String queue) throws JMSException {
    // Create a session for sending messages
	  System.out.println("phase 1 ");
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    System.out.println("phase 2 ");
    Destination destination = session.createQueue(queue);

    producer = session.createProducer(destination);
  }


  public void send(String text, int priority) throws JMSException {
    TextMessage message = session.createTextMessage(text);
    // Note: setting the priority directly on the JMS Message does not work
    // as in that case the priority of the producer is taken
    producer.send(message, DeliveryMode.PERSISTENT, priority, 0);
    session.close();
 

  }
}
Leave a Comment