JMS 메시지 큐 ActiveMQ (점 대 점 모드)

생산자 (producer) - > 메시지 큐 (message queue)
package com.java1234.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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;

/**
 *      
 * @author Administrator
 *
 */
public class JMSProducer {

	private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; //         
	private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; //        
	private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; //        
	private static final int SENDNUM=10; //        
	
	public static void main(String[] args) {
		
		ConnectionFactory connectionFactory; //     
		Connection connection = null; //   
		Session session; //               
		Destination destination; //       
		MessageProducer messageProducer; //      
		
		//        
		connectionFactory=new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
		
		try {
			connection=connectionFactory.createConnection(); //           
			connection.start(); //     
			session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //   Session
			destination=session.createQueue("FirstQueue1"); //       
			messageProducer=session.createProducer(destination); //        
			sendMessage(session, messageProducer); //     
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(connection!=null){
				try {
					connection.close();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 *     
	 * @param session
	 * @param messageProducer
	 * @throws Exception
	 */
	public static void sendMessage(Session session,MessageProducer messageProducer)throws Exception{
		for(int i=0;i 
  


(Consumer)--( )->

package com.java1234.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 *      
 * @author Administrator
 *
 */
public class JMSConsumer2 {

	private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; //         
	private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; //        
	private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; //        
	
	public static void main(String[] args) {
		ConnectionFactory connectionFactory; //     
		Connection connection = null; //   
		Session session; //               
		Destination destination; //       
		MessageConsumer messageConsumer; //       
		
		//        
		connectionFactory=new ActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKEURL);
				
		try {
			connection=connectionFactory.createConnection();  //           
			connection.start(); //     
			session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); //   Session
			destination=session.createQueue("FirstQueue1");  //          
			messageConsumer=session.createConsumer(destination); //        
			messageConsumer.setMessageListener(new Listener()); //       
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}

감청 (Listener) 메시지 큐
package com.java1234.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 *     
 * @author Administrator
 *
 */
public class Listener implements MessageListener{

	@Override
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		try {
			System.out.println("     :"+((TextMessage)message).getText());
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

좋은 웹페이지 즐겨찾기