Java中路由器模式

这是一个使用Java实现路由器模式的例子,展示了如何通过RMI创建一个路由器对象,并连接多个客户端GUI。每个客户端可以发送消息,路由器根据预定义的路由规则将消息转发到相应的目标客户端。
摘要由CSDN通过智能技术生成

 import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class RunRouterPattern {
  public static void main(String[] arguments) {
    System.out.println("Example for the Router pattern");
    System.out
        .println("This code same will create a series of GUIs, and use");
    System.out
        .println(" the Router pattern to map message notifications between");
    System.out
        .println(" them. In this code example, the Router will send messages");
    System.out
        .println(" between the GUI clients based on the following mapping:");
    System.out.println();
    System.out.println("/tGUI # 1:/tGUI #2/tGUI #3");
    System.out.println("/tGUI # 2:/tGUI #1/tGUI #4");
    System.out.println("/tGUI # 3:/tGUI #1/tGUI #4");
    System.out.println("/tGUI # 4:/tGUI #1/tGUI #2/tGUI #3/tGUI #4");
    System.out.println();

    System.out.println("Running the RMI compiler (rmic)");
    try {
      Process p1 = Runtime.getRuntime().exec("rmic Router");
      Process p2 = Runtime.getRuntime().exec("rmic RouterClient");
      p1.waitFor();
      p2.waitFor();
    catch (IOException exc) {
      System.err
          .println("Unable to run rmic utility. Exiting application.");
      System.exit(1);
    catch (InterruptedException exc) {
      System.err
          .println("Threading problems encountered while using the rmic utility.");
    }

    System.out.println("Starting the rmiregistry");
    System.out.println();
    Process rmiProcess = null;
    try {
      rmiProcess = Runtime.getRuntime().exec("rmiregistry");
      Thread.sleep(15000);
    catch (IOException exc) {
      System.err
          .println("Unable to start the rmiregistry. Exiting application.");
      System.exit(1);
    catch (InterruptedException exc) {
      System.err
          .println("Threading problems encountered when starting the rmiregistry.");
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值