在glassfish服务器上使用JMS发送文本消息 [英] Sending Text Message using JMS on glassfish server
问题描述
我正在用glassfish服务器测试JMS,因此我想在glassfish服务器队列上发送简单的文本消息。我曾尝试使用ActiveMQ,这很好,但我无法理解我可以在配置jndi.properties文件中放置哪些jar以及glassfish服务器需要哪个jar。请给我一些想法来实现这一点。
预先感谢
<由于您使用的是Glassfish,最简单的方法就是编写简单的应用程序(EJB)来执行任务。您必须在GF中定义:
- ConnectionFactory(资源 - > JMS资源 - >连接工厂),
让我们给它JNDI名称jms / ConnectionFactory
- 消息队列(资源 - > JMS资源 - >目标资源),
让我们给它JNDI名称jms / myQueue
下一步是在某些EJB中使用这些你需要写。这并不困难:首先,你必须注入:
$ $ $ $ $ c $ @Resource(mappedName =jms / ConnectionFactory)
private ConnectionFactory cf;
@Resource(mappedName =jms / myQueue)
private Queue messageQueue;
然后像这样使用它:
..
javax.jms.Connection conn = null;
javax.jms.Session s = null;
javax.jms.MessageProducer mp = null
try {
conn = cf.createConnection();
s = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);
mp = s.createProducer(messageQueue);
javax.jms.TextMessage msg = s.createTextMessage();
msg.setStringProperty(your-key,your-value);
msg.setText(您的短信);
mp.send(msg);
}
catch(JMSException ex){
//异常处理
}
finally {
try {
//关闭连接,会话和MessageProducer
} catch(JMSException ex){
//异常处理
}
}
关于配置,您不需要任何外部JAR,所有需要的东西都会发货。如果您不想编写EJB,但是需要定期使用Java(独立)应用程序,那么您必须包含 jms.jar
和 imq。 jar
。
I am testing JMS with glassfish server so for that i want to send simple text message on glassfish server queue. I have tried with ActiveMQ and that is going fine but i unable to understand what can i put in configuration jndi.properties file and which jar is needed for glassfish server. Please give me some idea to implement this.
thanks in advance
Since you're using Glassfish, the easiest way is to write simple application (EJB) that will perform the task. You have to define in GF:
- ConnectionFactory (Resources -> JMS Resources -> Connection Factory),
let's give it JNDI name
jms/ConnectionFactory
- Message queue (Resources -> JMS Resources -> Destination Resources),
let's give it JNDI name
jms/myQueue
Next step is to use these in some EJB that you need to write. It's not hard: firstly, you have to inject:
@Resource(mappedName="jms/ConnectionFactory")
private ConnectionFactory cf;
@Resource(mappedName="jms/myQueue")
private Queue messageQueue;
and then use it like this:
..
javax.jms.Connection conn = null;
javax.jms.Session s = null;
javax.jms.MessageProducer mp = null
try {
conn = cf.createConnection();
s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
mp = s.createProducer(messageQueue);
javax.jms.TextMessage msg = s.createTextMessage();
msg.setStringProperty("your-key", "your-value");
msg.setText("Your text message");
mp.send(msg);
}
catch(JMSException ex) {
// exception handling
}
finally {
try {
// close Connection, Session and MessageProducer
} catch (JMSException ex) {
//exception handling
}
}
Regarding configuration, you don't need any external JAR, everything that is needed is shipped. If you don't want to write EJB, but regular Java (standalone) application, then you'll have to include jms.jar
and imq.jar
.
这篇关于在glassfish服务器上使用JMS发送文本消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!