實戰JBuilder7+WebLogic7(四)續JMS+Message-Driven Bean (轉)

worldblog發表於2007-12-14
實戰JBuilder7+WebLogic7(四)續JMS+Message-Driven Bean (轉)[@more@]

由於CSDN不能顯示全部文章,要瀏覽全文請瀏覽以下連線

M.htm">

內容簡介:
1。連線MS SERVER2000
2。Session Bean
3。Entity Bean
4。JMS+Message-Driven bean
5。(待續)

全部實戰內容都經過嚴格測試,決無問題,只需嚴格按照文中步驟即可!

ideograph; MARGIN: 13pt 0cm; LINE-HEIGHT: 170%; TEXT-ALIGN: justify; mso-pagination: lines-together">實戰4JMS:namespace prefix = o ns = "urn:schemas--com::office" />

LOGIC

1.  啟動WebLogic7

2.  開啟,在位址列中輸入:<

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 JMileStore,輸入以下資訊:

Configuration->General頁:

Name = MDBDemo Store

Directory: = F:beauser_projectsmyainJMSStores (任意存在的目錄即可)

單擊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 = x.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..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.Topiblisher;

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.PortableRemote;

 

/**

 * 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:

  *

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

  *

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

  * they are interpreted in this order:

  *

  * @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 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

  (待續)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993140/,如需轉載,請註明出處,否則將追究法律責任。

相關文章