Jboss Ejb MDB

 

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 .

 

关于activateConfigqueue-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>

 

首先给我的感觉就是这个xmljmx有关,mbean代表manage bean ,这个问题不大。

这个xml的作用就是instancequeue,名字为tutorial ( 可以自己改过 ) ,然后由jmx-console这个控制台统一管理,运行完这个程序可以在http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss.mq.destination%3Aservice%3DQueue%2Cname%3Dtutorial (希望你还没来的及更改上面的配置,我找不到能让你更快找到这个queue的地址了,太多了。)  看到这个配置文件上的queue jboss里已经有了几个queue了。

然后就是定义个jndiClient.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

buildrun 就行了。

看看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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值