在JBOSS_HOME\server\default\deploy\hornetq\hornetq-jms.xml后面加上
<queuename="testQueue">
<entryname="/queue/myqueue"/>
</queue>
<topicname="testTopic">
<entry name="/topic/mytopic"/>
</topic>
jms 实例
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName ="destinationType" , propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName ="destination" , propertyValue="queue/myqueue")
})
public class HelloListener implements MessageListener {
public void onMessage(Message msg) {
TextMessage text = (TextMessage)msg;
String str;
try {
str = text.getText();
System.out.println("监听到了,Content:" + str);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
客户端
public static void main(String[] args)throws Exception {
InitialContext cxt = new InitialContext();
QueueConnectionFactory factory = (QueueConnectionFactory)cxt.lookup("ConnectionFactory");
Queue queue = (Queue) cxt.lookup("queue/myqueue");
QueueConnection conn = factory.createQueueConnection();
QueueSession session = (QueueSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage text = session.createTextMessage("is_zhoufeng");
QueueSender sender = session.createSender(queue);
for (int i = 0; i < 100 ; i++) {
sender.send(text);
}
sender.close();
session.close();
conn.close();
}
@MessageDriven(activationConfig={
@ActivationConfigProperty(propertyName ="destinationType" , propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName ="destination" , propertyValue="topic/mytopic")
})
public class TopicListener implements MessageListener{
public void onMessage(Message message) {
TextMessage text = (TextMessage)message;
try {
System.out.println("Topic监听器1:content:" + text.getText() );
} catch (JMSException e) {
e.printStackTrace();
}
}
}
客户端
public class TopicTest {
public static void main(String[] args)throws Exception {
InitialContext cxt = new InitialContext();
TopicConnectionFactory factory =(TopicConnectionFactory)cxt.lookup("ConnectionFactory");
Topic topic = (Topic) cxt.lookup("topic/mytopic");
TopicConnection conn = factory.createTopicConnection();
TopicSession session = (TopicSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage text =session.createTextMessage("is_zhoufeng");
TopicPublisher sender = session.createPublisher(topic);
sender.send(text);
sender.close();
session.close();
conn.close();
}
}