ActiveMq 的queue和Topic的例子

阅读更多
package Client;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class DeliverQueue {
	
	public static void main(String[] args) {
		String brokerUrl = "tcp://127.0.0.1:61616";
    	String userName = "admin";
    	String pswd = "admin";
    	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    	
    	((ActiveMQConnectionFactory) connectionFactory).setBrokerURL(brokerUrl);
    	((ActiveMQConnectionFactory) connectionFactory)
		.setUserName(userName);
    	
    	((ActiveMQConnectionFactory) connectionFactory)
		.setPassword(pswd);
    	
    	try {
    		Connection conn = ((ActiveMQConnectionFactory) connectionFactory).createConnection();
    		
    		conn.start();
			
    		Session jmsSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    		
    		Destination destination = jmsSession.createQueue("MONKEY_Q");
    		MessageProducer producer = jmsSession.createProducer(destination);
    		
    		Message msg = jmsSession.createTextMessage("Monkey king."); 
    		producer.send(msg);
    		
    		producer.close();
    		jmsSession.close();
    		conn.close();
    		
    		System.out.println("sent msg :" + msg);
    		
    		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}

}



package Client;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * Hello world!
 *
 */
public class ReceiveQueue 
{
    public static void main( String[] args )
    {
    	String brokerUrl = "tcp://127.0.0.1:61616";
    	String userName = "admin";
    	String pswd = "admin";
    	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    	
    	((ActiveMQConnectionFactory) connectionFactory).setBrokerURL(brokerUrl);
    	((ActiveMQConnectionFactory) connectionFactory)
		.setUserName(userName);
    	
    	((ActiveMQConnectionFactory) connectionFactory)
		.setPassword(pswd);
    	
    	try {
    		Connection conn = ((ActiveMQConnectionFactory) connectionFactory).createConnection();
    		
    		conn.start();
			
    		Session jmsSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    		
    		Destination destination = jmsSession.createQueue("MONKEY_Q");
    		MessageConsumer comsumer = jmsSession.createConsumer(destination);
    		
    		Message msg = comsumer.receive(1000L);
    		
    		comsumer.close();
    		jmsSession.close();
    		conn.close();
    		
    		System.out.println(msg);
    		
    		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
 
    }
}




package Client;

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

import org.apache.activemq.ActiveMQConnectionFactory;

public class ReceiveTopic implements Runnable{
	private static final String brokerUrl = "tcp://127.0.0.1:61616";
	private static final String userName = "admin";
	private static final String pswd = "admin";
	private String threadName;
	
	ReceiveTopic(String name){
		threadName = name; 
	}
	
	@Override
	public void run() {
		
		ConnectionFactory connFactory;
		Connection conn = null;
		Session session;
		
		Destination dest;
		
		MessageConsumer consumer;
		
		connFactory = new ActiveMQConnectionFactory(userName,pswd,brokerUrl);
		try{
			
			conn = connFactory.createConnection();
			conn.start();
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			dest = session.createTopic("AllWorld");
			
			consumer = session.createConsumer(dest);
			
			while(true){
				TextMessage message = (TextMessage)consumer.receive(100*1000);
				if(null != message){
					System.out.println("Thread " + threadName + " has reveived the message . " + message.getText());
				}else{
					continue;
				}
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try{
				if(null != conn){
					conn.close();
				}
			}catch(Throwable ignore){
				
			}
		}
		
	}

	public static void main(String[] args) {
		
		ReceiveTopic r1 = new ReceiveTopic("Thread 1 ");
		ReceiveTopic r2 = new ReceiveTopic("Thread 2 ");
		ReceiveTopic r3 = new ReceiveTopic("Thread 3 ");
		
		Thread thread1 = new Thread(r1);
		Thread thread2 = new Thread(r2);
		Thread thread3 = new Thread(r3);
		
		thread1.start();
		thread2.start();
		thread3.start();
		
	} 
	
	
}






package Client;

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

import org.apache.activemq.ActiveMQConnectionFactory;

public class SendToTopic {
	private static final String brokerUrl = "tcp://127.0.0.1:61616";
	private static final String userName = "admin";
	private static final String pswd = "admin";
	private static final int SEND_NUMBER = 5;
	
	public static void sendMessage(Session session, MessageProducer producer)
	throws Exception
	{
		for(int i=1; i <= SEND_NUMBER ; i++){
			TextMessage message = session.createTextMessage("I am new a message, number " + i + ".");
			
			System.out.println("Send message " + i );
			producer.send(message);
			
		}
	}
	
	public static void main(String[] args) {
		ActiveMQConnectionFactory connFactory ;
		Connection conn = null;
		Session session;
		
		Destination dest;
		
		MessageProducer producer;
		connFactory = new ActiveMQConnectionFactory(
								userName,
								pswd,
								brokerUrl);
		
		
		try{
			conn = connFactory.createConnection();
			conn.start();

			session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
			
			dest = session.createTopic("AllWorld");
			
			producer = session.createProducer(dest);
			
			producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			
			sendMessage(session, producer);
			
			session.commit();
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try{
				if(null != conn){
					conn.close();
				}
			}catch(Throwable ignore){
				
			}
		}
		
		
	}
	
}




引用



junit
junit
test



log4j
log4j



javax.transaction
jta



javax.jms
jms



commons-lang
commons-lang



opensymphony
quartz



commons-httpclient
commons-httpclient



com.****.****
****-framework-core
1.3.0-SNAPSHOT



com.****.****
****-framework-activemq
1.3.0-SNAPSHOT



        org.slf4j
        slf4j-log4j12
        1.5.2
   



你可能感兴趣的:(activemq)