第一个JMS程序_JMS学习

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;



public class JMSDelivery {

    protected String m_userName;
    protected String m_passWord;
    protected String m_destinationName;
    protected String m_defaultProviderUrl;
    protected String m_connectionFactory;
    protected String m_providerContextFactory;
    
    protected Context m_jndiContext;
    protected Connection m_connection;
    protected Session m_session;
    protected Destination m_destination;
    
    
    protected boolean m_transacted=false;
    protected int  m_acknowlegeMode=Session.AUTO_ACKNOWLEDGE;
    protected int  m_transferMode;
    
    protected int QUEUEMODE=0;
    protected int TOPICMODE=1;
    
    public JMSDelivery(String providerContextFactory,String defaultProviderUrl,String userName,String passWord,String connectionFactory,String destinationName)
    {
    	initiaData(providerContextFactory, defaultProviderUrl, userName, passWord,connectionFactory,destinationName);
    }
    
	protected void initiaData(String providerContextFactory,String defaultProviderUrl,String userName,String passWord,String connectionFactory,String destinationName)
	{
    	try {
			this.m_providerContextFactory=providerContextFactory;
			this.m_defaultProviderUrl=defaultProviderUrl;
			this.m_userName=userName;
			this.m_passWord=passWord;
			this.m_connectionFactory=connectionFactory;
			this.m_destinationName=destinationName;
			Properties env=new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY, this.m_providerContextFactory);
			env.put(Context.PROVIDER_URL, this.m_defaultProviderUrl);
			env.put(Context.SECURITY_PRINCIPAL, this.m_userName);
			env.put(Context.SECURITY_CREDENTIALS, this.m_passWord);
			this.m_jndiContext=new InitialContext(env);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}
	protected void connect()
	{
		try {
			ConnectionFactory connectionFactory=(ConnectionFactory)this.m_jndiContext.lookup(this.m_connectionFactory);
			this.m_transferMode=(connectionFactory instanceof QueueConnectionFactory) ? QUEUEMODE:TOPICMODE;
			if(this.m_transferMode==QUEUEMODE)
			{
				QueueConnectionFactory queueConnectionFactory=(QueueConnectionFactory)connectionFactory;
				QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();
				this.m_session=queueConnection.createQueueSession(this.m_transacted, this.m_acknowlegeMode);
				this.m_destination=(Destination)this.m_jndiContext.lookup(this.m_destinationName);
				this.m_connection=queueConnection;
			}else
			{
				TopicConnectionFactory topicConnectionFactory=(TopicConnectionFactory)connectionFactory;
				TopicConnection topicConnection=topicConnectionFactory.createTopicConnection();
				this.m_session=topicConnection.createTopicSession(this.m_transacted, this.m_acknowlegeMode);
				this.m_destination=(Destination)this.m_jndiContext.lookup(this.m_destinationName);
				this.m_connection=topicConnection;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void disConnect()
	{
		try {
			if(this.m_session!=null)
			{
				this.m_session.close();
				this.m_session=null;
			}
			if(this.m_connection!=null)
			{
				this.m_connection.close();
				this.m_connection=null;
			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void postMessageToQueue(String message) throws JMSException
	{
		Queue queue=(Queue)this.m_destination;
		QueueSession queueSession=(QueueSession)this.m_session;
		QueueSender queueSender=null;
		TextMessage textMessage=queueSession.createTextMessage();
		textMessage.clearBody();
		textMessage.setText(message);
		try {
			queueSender=queueSession.createSender(queue);
			queueSender.send(textMessage);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			if(queueSender!=null)
			{
				queueSender.close();
			}
		}
	}
	protected void pulishMessageToTopic(String message)throws JMSException
	{
			Topic topic=(Topic)this.m_destination;
			TopicSession topicSession=(TopicSession)this.m_session;
			TopicPublisher topicPublisher=null;
			TextMessage textMessage=topicSession.createTextMessage();
			textMessage.clearBody();
			textMessage.setText(message);
		try {
			topicPublisher=topicSession.createPublisher(topic);
			topicPublisher.publish(textMessage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(topicPublisher!=null)
			{
				topicPublisher.close();
			}
		}
		
	}
	 protected void getMessageFromQueue() throws JMSException {
	       Queue queue = (Queue)this.m_destination;
	       QueueSession queueSession = (QueueSession)this.m_session;
	       MessageConsumer queueReceiver = queueSession.createReceiver(queue);
	       this.m_connection.start();
	       long timeout = Long.parseLong("2000");
	       TextMessage message = (TextMessage)queueReceiver.receive(timeout);
	       getMessage(message);
	       queueReceiver.close();
	       this.m_connection.stop();
	   }

	   protected void getMessageFromTopic() throws JMSException{
	       Topic topic = (Topic)this.m_destination;
	       TopicSession topicSession = (TopicSession)this.m_session;
	       MessageConsumer topicSubscriber = topicSession.createDurableSubscriber(topic, this.m_userName);
	       this.m_connection.start();
	       long timeout = Long.parseLong("2000");
	       TextMessage message = (TextMessage)topicSubscriber.receive(timeout);
	       getMessage(message);
	       topicSubscriber.close();
	       this.m_connection.stop();
	   }
	   protected  void getMessage(TextMessage textMessage) throws JMSException
	   {
		   System.out.println(textMessage.getText());
	   }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String providerContextFactory="com.evermind.server.rmi.RMIInitialContextFactory";
		String defaultProviderUrl="ormi://localhost:23791";
		String userName="oc4jadmin";
		String passWord="welcome1";
		String connectionFactory="com.agile.jms.QueueConnectionFactory";
		String destinationName="jms/demoQueue";
		JMSDelivery jmsDelivery=new JMSDelivery(providerContextFactory, defaultProviderUrl, userName, passWord, connectionFactory, destinationName);
		jmsDelivery.connect();
		try {
			jmsDelivery.getMessageFromQueue();
//			jmsDelivery.postMessageToQueue("1111111");
//			jmsDelivery.postMessageToQueue("2222222");
//			jmsDelivery.postMessageToQueue("3333333");
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jmsDelivery.disConnect();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值