GlassFish JMS 配置
物理目的地名称不能包含('/') ,
大家一般喜欢把它和 JNDI 的名称统一起来 (jms/topic), 可是不行
其他的配置只要点几下鼠标即可
代码 (NetBeans EE project :pserver:pprun@cvs.dev.java.net:/cvs/hjpetstore/MyJMS)
1. 服务器端 (MDB - the subscriber)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejb;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
*
* @author pprun
*/
@MessageDriven(mappedName = "jms/topic", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "clientId", propertyValue = "MyMessageBean"),
@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "MyMessageBean")
})
public class MyMessageBean implements MessageListener {
public MyMessageBean() {
}
MapMessage msg = null;
@Override
public void onMessage(Message message) {
try {
msg = (MapMessage) message;
System.out.println("----------------------");
System.out.println(msg.getString("lastname"));
System.out.println(msg.getString("firstname"));
System.out.println(msg.getString("id"));
System.out.println("----------------------");
} catch (JMSException ex) {
Logger.getLogger(MyMessageBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
2. client (Enterprise Client) - the message/topic producer
package myjms;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
/**
*
* @author pprun
*/
public class Main {
@Resource(mappedName="jms/topicFactory")
private static ConnectionFactory topicFactory;
@Resource(mappedName="jms/topic")
private static Topic topic;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Connection topicConnection = null;
Session session = null;
MapMessage message = null;
MessageProducer producer = null;
try {
topicConnection = topicFactory.createConnection();
session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(topic);
message = session.createMapMessage();
message.setString("lastname", "Pi");
message.setString("firstname", "Pprun");
message.setString("id", "1111");
producer.send(message);
} catch (Exception e) {
Logger.getLogger(Main.class.getCanonicalName()).log(Level.SEVERE, "error in jmx send...", e);
}
}
}
The Result (GlassFish log)
----------------------
Pi
Pprun
1111
----------------------
开源工程站点
https://hjpetstore.dev.java.net/source/browse/hjpetstore/MyJMS/