对jms不是很熟悉,弄了几天终于有了点思路。这里有两个程序、增加了个queue-example-service.xml配置文件很简单,复杂点的在以后在说。
package org.jboss.tutorial.mdb.bean;
import javax.ejb.messagedriven;
import javax.ejb.activationconfigproperty;
import javax.jms.message;
import javax.jms.messagelistener;
@messagedriven(activateconfig =
{
@activationconfigproperty(propertyname="destinationtype", propertyvalue="javax.jms.queue"),
@activationconfigproperty(propertyname="destination", propertyvalue="queue/tutorial/example")
})
//a destination is the object on the jbossmq server that clients
//use to send and receive messages. there are two types of
//destination objects, queues and topics.
public class examplemdb implements messagelistener
{
public void onmessage(message recvmsg)
{
system.out.println("—————-");
system.out.println("received message");
system.out.println("—————-");
}
}
英文注释是我在the jboss 4 application server guide找的,可以知道destination是在jboss 服务器里负责收发消息 ( message ) 的地方。destination根据消息发布方式的不同分两种:queues 和 topics .
topic发布允许一对多,或多对多通讯通道,消息的产生者被叫做publisher, 消息接受者叫做subscriber,故称为 发布/订阅(publish/subscribe)。
queue 是另外一种方式,仅仅允许一个消息传送给一个客户。一个发送者将消息放在消息队列中,接受者从队列中抽取并得到消息,消息就会在队列中消失。第一个接受者抽取并得到消息后,其他人就不能在得到它。又称为 点对点(point to point) .
关于activateconfig跟queue-example-service.xml有关,这里只要知道destination就行了。从程序可以看到其接口为messagelistener可猜出这个程序负责监听消息,收到消息后打印。就是这样 :)
client.java
package org.jboss.tutorial.mdb.client;
import javax.jms.queue;
import javax.jms.queueconnection;
import javax.jms.queueconnectionfactory;
import javax.jms.queuesender;
import javax.jms.queuesession;
import javax.jms.textmessage;
import javax.naming.initialcontext;
public class client
{
public static void main(string[] args) throws exception
{
queueconnection cnn = null;
queuesender sender = null;
queuesession session = null;
initialcontext ctx = new initialcontext();
queue queue = (queue) ctx.lookup("queue/tutorial/example");
//这里lookup的内容在queue-example-service.xml有定义jndi
queueconnectionfactory factory = (queueconnectionfactory) ctx.lookup("connectionfactory");
cnn = factory.createqueueconnection();
session = cnn.createqueuesession(false,//不需要事务
queuesession.auto_acknowledge);//自动接收消息
textmessage msg = session.createtextmessage("hello world");
sender = session.createsender(queue);
sender.send(msg);
system.out.println("message sent successfully to remote queue.");
}
}
client在这里的执行顺序是 queueconnectionfactoryà queueconnection à queuesession àqueuesender
如果这样说还是不清楚的话就要先补习下jms咯。呵呵,我也是这样的。
client的任务呢就是发送个消息,然后由服务器接收。
queue-example-service.xml
<?xml version="1.0" encoding="utf-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.queue"
name="jboss.mq.destination:service=queue,name=tutorial">
<attribute name="jndiname">queue/tutorial/example</attribute>
<depends optional-attribute-name="destinationmanager">jboss.mq:service=destinationmanager</depends>
</mbean>
</server>
首先给我的感觉就是这个xml跟jmx有关,mbean代表manage bean ,这个问题不大。
这个xml的作用就是instance个queue,名字为tutorial ( 可以自己改过 ) ,然后由jmx-console这个控制台统一管理,运行完这个程序可以在http://localhost:8080/jmx-console/htmladaptor?action=inspectmbean&name=jboss.mq.destination%3aservice%3dqueue%2cname%3dtutorial (希望你还没来的及更改上面的配置,我找不到能让你更快找到这个queue的地址了,太多了。) 看到这个配置文件上的queue ,jboss里已经有了几个queue了。
然后就是定义个jndi,client.java就可以lookup了。
这里附上log4j.properties 在jboss-ejb-3.0_preview_5.zip 里面没有这个老是显示缺少appender。有了这个将在该目录下生成个record.log日志文件。
log4j.properties
log4j.appender.r=org.apache.log4j.rollingfileappender
log4j.appender.r.file=record.log
log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%p %d{hh:mm:ss} %t %c{1} -%m%n
log4j.appender.r.maxbackupindex=1
log4j.appender.r.maxfilesize=100kb
log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l) -%m%n
log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.rootlogger=stdout,r
运行:参考installing.html
windows下
打开命令提示符cmd,到 jboss_home/bin
run.bat –c all
用ant
先build后run 就行了。
看看jboss窗口可以看到
01:01:20,828 info [stdout] —————-
01:01:20,828 info [stdout] received message
01:01:20,828 info [stdout] —————-
讨论:
虽然就两个程序,但是由于我以前没怎么了解jms 就花了些时间。查找相关的资料对于理解以上问题是很重要。
http://www.cn-java.com/target/news.php?news_id=2730
http://blog.blogchina.com/category.225381.html
the jboss 4 application server guide
