【问题标题】:How to check server is up?如何检查服务器是否已启动?
【发布时间】:2012-02-17 19:47:48
【问题描述】:

我在两台不同的物理机器上有一个由两个服务器节点组成的 IBM Websphere Application Server 集群。谁能帮我用 java 代码检查我的服务器实例是否正在运行或其中一个服务器没有启动和运行?

【问题讨论】:

  • WebSphere ND 不提供开箱即用的功能吗?
  • 我的目标是在服务器宕机/挂起时得到通知。你能告诉我 WAS ND 是如何提供这些细节的吗?
  • 只需请求一个已知页面并确保将其取回;琐碎的 cron 脚本之类的。
  • 检查我的答案......希望这是你想要的......
  • @Pangea 可能只是指 WebSphere Integrated Solutions Console,它提供了一个基于浏览器的图形工具来管理 WAS,包括轻松查看集群、服务器和应用程序状态的能力。但是,由于您请求了基于 Java 的方法并提到了通知,因此您可能需要考虑 WebSphere 管理 Java 客户端,正如我在 below 中所描述的那样。

标签: java websphere


【解决方案1】:

要快速且可移植地执行此操作,您可以检查页面是否由服务器提供。

例如,您可以:

boolean isAlive = true;
try {
  URL hp = new URL("http://yourserver/TestPage.html"); 
  URLConnection hpCon = hp.openConnection(); 
  // add more checks...
} catch (Exception e) {
  isAlive = false;
}

这种不太复杂的方法适用于每个 http 服务器。

【讨论】:

  • @Andrea Colleoni:我相信boolean isAlive = false; 应该是boolean isAlive = true;.. 对吧?
  • 这缺少实际建立连接的 hpCon.connect() 调用。
【解决方案2】:

希望下面是你想要的……

URL url = new URL( "http://google.com/" );
HttpURLConnection httpConn =  (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" ); 
httpConn.connect();

System.out.println( "google.com : " + httpConn.getResponseCode());

或失败:

URL url = new URL( "http://google.com:666/" );
HttpURLConnection httpConn =  (HttpURLConnection)url.openConnection();
httpConn.setInstanceFollowRedirects( false );
httpConn.setRequestMethod( "HEAD" ); 
try{
    httpConn.connect();
     System.out.println( "google.com : " + httpConn.getResponseCode());
}catch(java.net.ConnectException e){
     System.out.println( "google.com:666 is down ");
}

祝你好运!!!

【讨论】:

    【解决方案3】:

    我认为您可能正在寻找的是 WebSphere Thin Administrative Client 的使用,它公开了 Java API 并提供了对 WAS MBeans 的访问,允许您查询服务器/应用程序的状态(以及许多其他管理和监控任务)。

    首先,您需要获得到 WAS(AdminClient)的连接,如下所示:

    Properties clientProps = new Properties();
    clientProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
    clientProps.setProperty(AdminClient.CONNECTOR_HOST, dmgrHostname);
    clientProps.setProperty(AdminClient.CONNECTOR_PORT, dmgrSoapConnectorPort);
    if (dmgrIsSecure) {
        clientProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
        clientProps.setProperty(AdminClient.USERNAME, wasUsername);
        clientProps.setProperty(AdminClient.PASSWORD, wasUserPassword);
    }
    AdminClient adminClient = AdminClientFactory.createAdminClient(clientProps);
    

    接下来,您需要查询相关的 MBean,然后执行相关操作。在您的情况下,您可能对ClusterMgr 和/或J2EEApplication MBean 感兴趣。下面是一个查询集群状态的例子:

    AdminClient adminClient = getAdminClient(target);
    ObjectName clusterMgr =
        (ObjectName)adminClient.queryNames(
            ObjectName.getInstance("WebSphere:*,type=ClusterMgr"), null).iterator().next();
    String state = adminClient.invoke(clusterMgr, "getClusterState",
        new Object[] {clusterName}, new String[] {String.class.getName()});
    

    您可以根据需要调用进一步的操作,例如查询单个集群成员的状态。

    此外,除了查询之外,您还可以register notifications 以便在某些事件发生时通知您的程序,例如集群、服务器或应用程序的状态变化。

    【讨论】:

      【解决方案4】:

      我们使用openConnection() 获得我们想要的URL 的专用连接。它将返回抽象类URLConnection 的子类,具体取决于URL 的公共协议,例如HttpURLConnection。然后用connect()的方法打开通讯链接

      private String server = "http://testserver:9086";
      try {
          URLConnection hpCon = new URL(SERVER).openConnection();
          hpCon.connect();
      } catch (Exception e) {
          // Anything you want to do when there is a failure
      }
      

      【讨论】:

      • @Vogel612 代码答案很好。放松。
      • @Vogel612 感谢您的反馈。我有同样的问题并想分享代码,我认为它很清楚,但肯定可以更清楚。现在我试着解释一下代码。
      猜你喜欢
      • 2019-11-12
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多