📄 jmsconnector.java
字号:
package book.jmx.examples;
import java.io.*;
import java.util.*;
import javax.management.*;
import javax.jms.*;
import javax.naming.*;
public class JMSConnector implements JMSConnectorConstants {
// JMS topic
private TopicConnection connection = null;
private TopicSubscriber subscriber = null;
private Topic topic = null;
// JNDI names for connection factory and topic
private String connFactory = "TopicConnectionFactory";
private String topicName = "topic/JMSConnector";
// reference to the target MBean server
private MBeanServer server = null;
// constructor
public JMSConnector() { }
public void start(String agentID) throws NamingException, JMSException {
server = (MBeanServer)MBeanServerFactory
.findMBeanServer(agentID).get(0);
// lookup topic and create topic connection
InitialContext ctx = new InitialContext();
TopicConnectionFactory fact =
(TopicConnectionFactory)ctx.lookup(connFactory);
topic = (Topic)ctx.lookup(topicName);
connection = fact.createTopicConnection();
// subscribe to invocation messages
TopicSession session = connection.createTopicSession(
false, Session.AUTO_ACKNOWLEDGE);
subscriber = session.createSubscriber(topic, "JMSType='SEND'", true);
subscriber.setMessageListener(new ConnectorListener());
connection.start();
}
/* Export Manager */
public Properties getExportProperties() {
Properties props = new Properties();
props.put(JMS_TOPIC_CONNECTION_FACTORY, connFactory);
props.put(JMS_TOPIC, topicName);
return props;
}
// message listener for invocations
class ConnectorListener implements MessageListener {
TopicSession session = null;
TopicPublisher publisher = null;
// constructor
ConnectorListener() throws JMSException {
// create a publisher session for return values
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
publisher = session.createPublisher(topic);
publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
publisher.setDisableMessageTimestamp(true);
}
// onMessage invoke when a message arrives
public void onMessage(Message message) {
try {
// extract MethodInvocation and message ID
ObjectMessage msg = (ObjectMessage)message;
MethodInvocation mi = (MethodInvocation)msg.getObject();
String ID = msg.getStringProperty("ID");
// set the target mbean server reference and invoke the method
mi.setMBeanServer(server);
mi.invoke();
// Wrap the invoked MI as JMS object message and send it back.
// The MI will contain the return value. Attach the same ID
// to the return message.
ObjectMessage reply = session.createObjectMessage(mi);
reply.setJMSType("REPLY");
reply.setStringProperty("ID", ID);
// send message
publisher.publish(reply);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -