⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 weblogic应用全实例
💻 JAVA
字号:
//声明本类定义在包examples.ejb20.message中
package examples.ejb20.message;
//声明本类引入的其它类
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;

/**
 * 这个类演示调用一个消息驱动的bean并在主题消息中发布报价。
 *
 */

public class Client {
  static private String TOPIC_NAME = "quotes";
  //声明服务器url
  private String m_url;
  //声明上下文
  private Context m_context;
  //声明主题连接
  private TopicConnection m_topicConnection;
  //构造方法
  public Client(String url)
    throws NamingException
  {
    m_url = url;
        
    try {
      //
      // 创建上下文
      //
      m_context = getInitialContext();
      
      //
      // 创建连接并启动
      //
      TopicConnectionFactory cf =
        (TopicConnectionFactory) m_context.lookup("weblogic.jms.ConnectionFactory");
      m_topicConnection = cf.createTopicConnection();
      m_topicConnection.start();
      
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }


  /**
   * 用命令行运行本实例:
   * java examples.ejb20.message.Client "t3://localhost:7001"
   * @参数 url               URL 如 "t3://localhost:7001" 
   */
  public static void main(String[] args) throws Exception {

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

    String url       = "t3://localhost:7001";
    
    // 解析命令行参数
     if (args.length != 1) {
      System.out.println("Usage: java examples.ejb20.message.Client t3://hostname:port");   
      return;
    } else {
      url = args[0];
    }

    Client client = null;
    try {
    //本类实例
      client = new Client(url);
    } catch (NamingException ne) {
      System.exit(1);
    }

    try {
    //调用例程
      client.example();
    }
    catch (Exception e) {
    //异常处理
      log("There was an exception while creating and using the Trader.");
      log("This indicates that there was a problem communicating with the server: "+e);
      //e.printStackTrace();
    } 

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

  /**
   * 运行例程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"
    };
    for (int i = 0; i < quotes.length; i++) {
    //发送消息
      tm.setText(quotes[i]);
      sender.publish(tm);
    }
  }


  /**
   * 初始化上下文
   */
  private Context getInitialContext() throws NamingException {
    
    try {
      // 属性对象
      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;
    }
  }

  /**
   * 另一个版本,使用jndi.properties file文件。
   *
   */
//    private static Context getInitialContext()
//      throws NamingException
//    {
//      return new InitialContext();
//    }

  private static void log(String s) {
    System.out.println(s);
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -