ejb学习笔记--jms和消息驱动bean

近日学习ejb,,,做点笔记的好


//先在ejb的deploy添加 memorycat-serive.xml

<?xml version="1.0" encoding="utf-8" ?>
<server>
	<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=MemoryCatQueue">
		<attribute name="JNDIName">queue/MemoryCatQueue</attribute>
		<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>    
 	</mbean> 
	
	
	<mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Topic,name=MemoryCatTopic">
		<attribute name="JNDIName">topic/MemoryCatTopic</attribute>
		<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>    
 	</mbean> 
	
</server>


//QueueSender.java

package com.memorycat.ejb.sender;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSender
{

	public static void main(String[] args) throws NamingException, JMSException
	{
		// TODO Auto-generated method stub
		InitialContext initialContext = new  InitialContext();
		QueueConnectionFactory queueConnectionFactory=(QueueConnectionFactory)initialContext.lookup("QueueConnectionFactory");
		QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
		QueueSession queueSession=queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
		Destination destination= (Destination) initialContext.lookup("queue/MemoryCatQueue");
		MessageProducer messageProducer = queueSession.createProducer(destination);
		messageProducer.send(queueSession.createTextMessage("hello,java Message Serive!"));
		queueSession.close();
		queueConnection.close();
		 
	}

}
//QueueListener.java

package com.memorycat.ejb.revicer;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MemoryCatQueue"), })
public class QueueListener implements MessageListener
{

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

}

Topic 类型差不多,就不发出来,,具体可以参考附件ejbjms.zip



评论
文章信息
作者: admin
目录: java
创建: 2014-08-05
更新: 2014-08-05
如果文章对你有帮助,请博主喝杯咖啡:-)