Jbuilder8开发J2ee学习笔记(6) (转)

Jbuilder8开发J2ee学习笔记(6) (转)[@more@]

Jbuilder8开发J2EE学习笔记(6)XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

    作者:缪青

开发JMS

1.建立JMS

首先建立JMS,由于需要调用SessionBean所以加入一个Field“sessHome”,注意类型一定是SessHome。对于JMS的一些参数都是我们在前面webLOGIC6.1配置中所提前配置好的,忘了的话,回去看看。

ASPectratio="t">

 .NET/develop/article/images/clip_image001_19269.png" align=baseline border=0>

这样一个简单的点对点的JMS接收器就差不多了。

2.书写代码

  public void onMessage(Message msg) {

  try {

  TextMessage TMS = (TextMessage) msg;

  String a1 = TMS.getText();

  String b1 = a1;

  System.out.println("Message getted Is :" + a1 + ">>>>>>>>>>>OK");

  Context context = getInitialContext();

  object ref = context.lookup("SessBean");

  sessHome = (SessHome) PortableRemoteObject.narrow(ref, SessHome.class);

  sessHome.create().insTableAa11(a1, b1);

  //这是通过Session的insTableAa11调用EntityBean的EJBHomeEjbInsertAa11

  //功能其是很简单,插入Aa11表一条记录(a1,a1)

  }

  catch (Exception ex) {

  ex.printStackTrace();

  }

  }

代码很简单,几乎和SessionBean的测试端代码差不多,没什么问题吧。

3.发布JMS

和上一节讲的一样,使用ReDelpoy发布EJB上Weblogic6.1

4.开发测试程序

现在要开发JMS的发布端了,这里只要新建一个CLASS就可以了。

代码下面给出:

package userjmssesenbdb;

import Java.util.*;

import javax.jms.*;

import javax.naming.*;

 

public class Client {

  public final static String

  JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

  public final static String URL = "t3://127.0.0.1:7001";

  private static final String QUEUE_CONNECTION_FACTORY =

  "DemoJMSCOnnectionFactory";

  private static final String QUEUE = "DemoJMSQueue";

  private static InitialContext getInitialContext(String url) throws

  NamingException {

  Hashtable env = new Hashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

  env.put(Context.PROVideR_URL, url);

  return new InitialContext(env);

  }

 

  public static void main(String[] args) throws Exception {

  Context ctx = getInitialContext(URL);

  QueueConnectionFactory qConnFact =

  (QueueConnectionFactory) ctx.lookup(QUEUE_CONNECTION_FACTORY);

  QueueConnection qConn = qConnFact.createQueueConnection();

  QueueSession qSess = qConn.createQueueSession(false,

  Session.AUTO_ACKNOWLEDGE);

  Queue q = (Queue) ctx.lookup(QUEUE);

  QueueSender qSend = qSess.createSender(q);

 

  TextMessage txtMsg = qSess.createTextMessage("c");

  System.out.println("Sending a message to queue");

  qSend.send(txtMsg);

  //---------------------------------------------------------//

  txtMsg = qSess.createTextMessage("a");

  System.out.println("Sending a message to queue");

  qSend.send(txtMsg);

  //---------------------------------------------------------//

  qConn.close();

  System.exit(0);

  }

}

看懂了么,其实就是发送“c”,“a”到接收端。执行一下,是不是数据表aa11中又多了2条记录。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-997886/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-997886/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值