免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3820 | 回复: 0
打印 上一主题 下一主题

[WebLogic] [zt]实战JBuilder7+WebLogic7(四)续JMS+Message-Driven Bean [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2002-11-22 22:30 |只看该作者 |倒序浏览
实战JBuilder7+WebLogic7(四)续JMS+Message-Driven Bean    liuxiaowei(原作)
  
关键字     JBuilder7 WebLogic7 JMS Message-Driven Bean
  


由于CSDN不能显示全部文章,要浏览全文请浏览以下连接

http://weisoft.myrice.com/Docs/JMSMDB.htm

内容简介:
1。连接MS SQL Server2000
2。Session Bean
3。Entity Bean
4。JMS+Message-Driven bean
5。JSP调用EJB(待续)

全部实战内容都经过严格测试,决无问题,只需严格按照文中步骤即可!

实战4:JMS
配置Weblogic

1.    启动WebLogic7

2.    打开IE6,在地址栏中输入:<http://localhost:7001/console>;

3.    输入用户名和密码

4.    在左边的目录树中选中Services->;JMS->;Connection Factories,单击右侧的Configure a new JMS Connection Factory ,输入以下信息:

Configuration->;General页:

Name = MDBDemo Connection Factory

JNDIName= MDBDemoCF

其它不变,单击Create建立Connection Factory。

Targets->;Server页:

将myserver(服务器名称)移至右侧的列表中,但击单击Apply

5.    在左边的目录树中选中Services->;JMS->;Stores,单击右侧的Configure a new JMSFileStore,输入以下信息:

Configuration->;General页:

Name = MDBDemo Store

Directory: = F:\bea\user_projects\mydomain\JMSStores (任意存在的目录即可)

单击Create建立JMSFileStore。

6.       Services->;JMS->;Servers,单击右侧的Configure a new JMS Connection Factory ,输入以下信息:

Configuration->;General页:

Name = MDBDemo JMSServer

Store = MDBDemo Store

其它不变,单击Create建立JMS Server。

Targets->;Servers页:

Target = myserver(你的weblogic server的名字)

单击Configuration->;General页中的Configure Destinations

Name = MDBDemo Topic

JNDIName= MDBDemo Topic

其它不变,单击Create建立Destination。

配置完毕。

建立Message Driven Bean:

1. 关闭所有工程:File->;Close Projects

2. 选择File->;New project

3. 在Name栏中输入MDBDemo,Directory栏中输入存放路径(不要有空格),其他不变,单击Finish。

4. 选择File->;New->;Enterprise->;EJB Module单击OK。

5. 在弹出的对话框中,在Name中输入MDBMoudle, Version选择:EJB2.0 Compliant其余不变,单击OK关闭当前对话框。

6. 在右侧的EJB Designer 中单击鼠标右键选择:Create EJB->;Message-Driven Bean,按如下填写:

Bean Name = MDBDemo

Transaction Type = Container

Destination Name = MDBDemo Topic

Destination Type = javax.jms.Topic

其它不变。

7.Project->;Make ”MDBModule”, 编译成功后,右键单击左上角的MDBModule选择Deploy Options for ”MDBModule.jar”->;Deploy,将其发布至Weblogic。

建立客户端:

以下是客户端源码,保存成TestClient.java加入工程后,选择Run->;Run “TestClient.java” using defaults运行即可,可以在weblogic console窗口看到输出。如果看不到输出,重起weblogic试一试。

package mdbdemo&#59;



import java.rmi.RemoteException&#59;

import java.util.Properties&#59;



import javax.jms.JMSException&#59;

import javax.jms.Message&#59;

import javax.jms.Session&#59;

import javax.jms.TextMessage&#59;

import javax.jms.Topic&#59;

import javax.jms.TopicConnection&#59;

import javax.jms.TopicConnectionFactory&#59;

import javax.jms.TopicPublisher&#59;

import javax.jms.TopicSession&#59;







import javax.ejb.CreateException&#59;

import javax.ejb.RemoveException&#59;

import javax.naming.Context&#59;

import javax.naming.InitialContext&#59;

import javax.naming.NamingException&#59;

import javax.rmi.PortableRemoteObject&#59;



/**

* This class illustrates calling a Message-Driven bean and publishing

* quotes on a topic.

*

* @author Copyright (c) 1998-2002 by BEA Systems, Inc. All Rights Reserved.

*/



public class TestClient {

  static private String TOPIC_NAME = &quot;MDBDemo Topic&quot;&#59;



  private String m_url&#59;



  private Context m_context&#59;

  private TopicConnection m_topicConnection&#59;



  public TestClient(String url)

    throws NamingException

  {

    m_url = url&#59;



    try {

      //

      // Create a context

      //

      m_context = getInitialContext()&#59;



      //

      // Create the connection and start it

      //

      TopicConnectionFactory cf =

        (TopicConnectionFactory) m_context.lookup(&quot;MDBDemoCF&quot&#59;

      m_topicConnection = cf.createTopicConnection()&#59;

      m_topicConnection.start()&#59;



    }

    catch(Exception ex) {

      ex.printStackTrace()&#59;

    }

  }





  /**

   * Runs this example from the command line. Example:

   * <p>;

   * <tt>;java examples.ejb20.message.Client &quot;t3://localhost:7001&quot;</tt>;

   * <p>;

   * The parameters are optional, but if any are supplied,

   * they are interpreted in this order:

   * <p>;

   * @param url               URL such as &quot;t3://localhost:7001&quot; of Server

   */

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



    log(&quot;\nBeginning message.Client...\n&quot&#59;



    String url       = &quot;t3://localhost:7001&quot;&#59;



    TestClient client = null&#59;

    try {

      client = new TestClient(url)&#59;

    } catch (NamingException ne) {

      System.exit(1)&#59;

    }



    try {

      client.example()&#59;

    }

    catch (Exception e) {

      log(&quot;There was an exception while creating and using the MDB.&quot&#59;

      log(&quot;This indicates that there was a problem communicating with the server: &quot;+e)&#59;

      //e.printStackTrace()&#59;

    }



    log(&quot;\nEnd message.Client...\n&quot&#59;

  }



  /**

   * Runs this example.

   */

  public void example()

    throws RemoteException, JMSException, NamingException

  {

    Topic newTopic = null&#59;

    TopicSession session = null&#59;

    try {

      session =

        m_topicConnection.createTopicSession(false,   // non transacted

                                             Session.AUTO_ACKNOWLEDGE)&#59;



      newTopic = (Topic) m_context.lookup(TOPIC_NAME)&#59;

    }

    catch(NamingException ex) {

      newTopic = session.createTopic(TOPIC_NAME)&#59;

      m_context.bind(TOPIC_NAME, newTopic)&#59;

    }



    TopicPublisher sender = session.createPublisher(newTopic)&#59;

    TextMessage tm = session.createTextMessage()&#59;

    String[] quotes = new String[] {

      &quot;BEAS 40 1/8&quot;, &quot;SUNW 79 1/2&quot;, &quot;IBM 82 1/4&quot;, &quot;Hello !&quot;

    }&#59;

    for (int i = 0&#59; i < quotes.length&#59; i++) {

      tm.setText(quotes)&#59;

      sender.publish(tm)&#59;

    }

  }





  /**

   * Using a Properties object will work on JDK 1.1.x and Java2

   * clients

   */

  private Context getInitialContext() throws NamingException {



    try {

      // Get an InitialContext

      Properties h = new Properties()&#59;

      h.put(Context.INITIAL_CONTEXT_FACTORY,

        &quot;weblogic.jndi.WLInitialContextFactory&quot&#59;

      h.put(Context.PROVIDER_URL, m_url)&#59;

      return new InitialContext(h)&#59;

    }

    catch (NamingException ex) {

      log(&quot;We were unable to get a connection to the WebLogic server at &quot;+m_url)&#59;

      log(&quotlease make sure that the server is running.&quot&#59;

      throw ex&#59;

    }

  }



  /**

   * This is the Java2 version to get an InitialContext.

   * This version relies on the existence of a jndi.properties file in

   * the application's classpath.

   *

   */

//    private static Context getInitialContext()

//      throws NamingException

//    {

//      return new InitialContext()&#59;

//    }



  private static void log(String s) {

    System.out.println(s)&#59;

  }



}


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP