ITPub博客

首页 > 应用开发 > IT综合 > Jbuilder8开发J2ee学习笔记(6) (转)

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

原创 IT综合 作者:gugu99 时间:2007-08-15 13:37:41 0 删除 编辑
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博客 ” ,链接:https://blog.itpub.net/10748419/viewspace-958007/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3121
  • 访问量
    2643809