jboss6 jms 配置使用


二话不说上代码

jboss-6.1.0.Final\server\all\deploy\hornetq\hornetq-jms.xml

加入配置  

 
     
  

topic配置为一对多。即一条消息发出去,可以由多个MessageListener接收处理。

也可配置为点对点queue。

服务端建立messagebean

package com.message;

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(mappedName = "jms/MyMessageCenter", activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), 
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "/topic/MsgTopic")
})
public class MyMessageCenter implements MessageListener {

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

发布到deploy下

客户端调用

package com;

import java.util.Properties;

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 javax.naming.InitialContext;
import javax.naming.NamingException;

public class test {
public static void main(String[] args){
	Properties prop=new Properties();
	prop.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
	prop.setProperty("java.naming.provider.url", "localhost:1100");
	prop.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
	InitialContext ctx;
	try {
		ctx=new InitialContext(prop);
		ConnectionFactory cf = (ConnectionFactory) ctx
		.lookup("ConnectionFactory");
		Connection con = cf.createConnection();
		Session session = con
		.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination dest = (Destination) ctx.lookup("/topic/MsgTopic");
		MessageProducer pro = session.createProducer(dest);
		TextMessage message=session.createTextMessage("hello message!!!");
		pro.send(message);
		session.close();
	} catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

}

控制台显示
hello message!!!--------------3

ok。

其实这些配置可以再jboss的管理页面完成配置的。即http://localhost:8080/admin-console/

你可能感兴趣的:(jboss6)