实战JBuilder7+WebLogic7(四)续JMS+Message-Driven Bean

类别:Java 点击:0 评论:0 推荐:

由于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(待续)

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

实战4JMS

配置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;

 

import java.rmi.RemoteException;

import java.util.Properties;

 

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.Session;

import javax.jms.TextMessage;

import javax.jms.Topic;

import javax.jms.TopicConnection;

import javax.jms.TopicConnectionFactory;

import javax.jms.TopicPublisher;

import javax.jms.TopicSession;

 

 

 

import javax.ejb.CreateException;

import javax.ejb.RemoveException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

 

/**

 * 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 = "MDBDemo Topic";

 

  private String m_url;

 

  private Context m_context;

  private TopicConnection m_topicConnection;

 

  public TestClient(String url)

    throws NamingException

  {

    m_url = url;

 

    try {

      //

      // Create a context

      //

      m_context = getInitialContext();

 

      //

      // Create the connection and start it

      //

      TopicConnectionFactory cf =

        (TopicConnectionFactory) m_context.lookup("MDBDemoCF");

      m_topicConnection = cf.createTopicConnection();

      m_topicConnection.start();

 

    }

    catch(Exception ex) {

      ex.printStackTrace();

    }

  }

 

 

  /**

   * Runs this example from the command line. Example:

   * <p>

   * <tt>java examples.ejb20.message.Client "t3://localhost:7001"</tt>

   * <p>

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

   * they are interpreted in this order:

   * <p>

   * @param url               URL such as "t3://localhost:7001" of Server

   */

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

 

    log("\nBeginning message.Client...\n");

 

    String url       = "t3://localhost:7001";

 

    TestClient client = null;

    try {

      client = new TestClient(url);

    } catch (NamingException ne) {

      System.exit(1);

    }

 

    try {

      client.example();

    }

    catch (Exception e) {

      log("There was an exception while creating and using the MDB.");

      log("This indicates that there was a problem communicating with the server: "+e);

      //e.printStackTrace();

    }

 

    log("\nEnd message.Client...\n");

  }

 

  /**

   * Runs this example.

   */

  public void example()

    throws RemoteException, JMSException, NamingException

  {

    Topic newTopic = null;

    TopicSession session = null;

    try {

      session =

        m_topicConnection.createTopicSession(false,   // non transacted

                                             Session.AUTO_ACKNOWLEDGE);

 

      newTopic = (Topic) m_context.lookup(TOPIC_NAME);

    }

    catch(NamingException ex) {

      newTopic = session.createTopic(TOPIC_NAME);

      m_context.bind(TOPIC_NAME, newTopic);

    }

 

    TopicPublisher sender = session.createPublisher(newTopic);

    TextMessage tm = session.createTextMessage();

    String[] quotes = new String[] {

      "BEAS 40 1/8", "SUNW 79 1/2", "IBM 82 1/4", "Hello !"

    };

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

      tm.setText(quotes[i]);

      sender.publish(tm);

    }

  }

 

 

  /**

   * 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();

      h.put(Context.INITIAL_CONTEXT_FACTORY,

        "weblogic.jndi.WLInitialContextFactory");

      h.put(Context.PROVIDER_URL, m_url);

      return new InitialContext(h);

    }

    catch (NamingException ex) {

      log("We were unable to get a connection to the WebLogic server at "+m_url);

      log("Please make sure that the server is running.");

      throw ex;

    }

  }

 

  /**

   * 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();

//    }

 

  private static void log(String s) {

    System.out.println(s);

  }

 

}

实战5JSP调用EJB

    (待续)

本文地址:http://com.8s8s.com/it/it18120.htm