我写了一个HBase连接器,如下所示。配置设置的目的是快速失败,如果HBase主机关闭,我们希望尽快抛出错误,以便用户不必永远等待。如果我在主机未启动时使用以下代码作为独立的java程序,它会在几秒后抛出异常,这就是我们想要的。
package mypackage.connector;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
public class Connector {
public void connectAndGetTable() throws IOException{
String tableName = "test";
String hNode = "192.168.56.103";
// Hbase Configurations
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", hNode);
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.rootdir", "hdfs://192.168.56.103:8020/apps/hbase/data");
conf.set("zookeeper.znode.parent", "/hbase-unsecure");
conf.set("hbase.rpc.timeout", "1000");
conf.set("hbase.client.retries.number", "1");
conf.set("zookeeper.session.timeout", "100");
conf.set("zookeeper.recovery.retry", "1");
conf.set("hbase.client.pause", "100");
conf.set("hbase.client.operation.timeout", "100");
// create an hbase connection
HConnection connection = HConnectionManager.createConnection(conf);
// get table handle from hbase connection
HTableInterface table = connection.getTable(tableName);
table.close();
}
}
但是,如果我把它放在Tomcat中,并在jsp页面中这样访问它:
<!DOCTYPE html>
<html>
<head><title>Sample JSP Page</title></head>
<body>
<h1>Sample JSP Page</h1>
<%@ page import="java.util.*" %>
<%@page import="mypackage.connector.Connector"%>
<%@page import="org.apache.hadoop.*"%>
<%
Connector connector=new Connector();
connector.connectAndGetTable();
%>
</body></html>
当我点击jsp页面时,几秒后会抛出以下异常,这就是我们想要的:
Stacktrace:
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:467)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.hadoop.hbase.client.NoServerForRegionException: Unable to find region for after 1 tries.
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegionInMeta(HConnectionManager.java:1194)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:1110)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:1067)
at org.apache.hadoop.hbase.client.HTable.finishSetup(HTable.java:326)
at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:310)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getTable(HConnectionManager.java:763)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getTable(HConnectionManager.java:745)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getTable(HConnectionManager.java:735)
at loblaw.connector.Connector.connectAndGetTable(Connector.java:32)
at org.apache.jsp.hello_jsp._jspService(hello_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
... 23 more
然而,在此之后,它尝试使用以下日志永远重新连接到HBase:
2015-06-01 13:24:30 INFO ClientCnxn:1083 - Client session timed out, have not heard from server in 1103ms for sessionid 0x0, closing socket connection and attempting reconnect
2015-06-01 13:24:31 INFO ClientCnxn:966 - Opening socket connection to server 192.168.56.103/192.168.56.103:2181. Will not attempt to authenticate using SASL (unknown error)
2015-06-01 13:24:31 INFO ClientCnxn:1083 - Client session timed out, have not heard from server in 1102ms for sessionid 0x0, closing socket connection and attempting reconnect
2015-06-01 13:24:32 INFO ClientCnxn:966 - Opening socket connection to server 192.168.56.103/192.168.56.103:2181. Will not attempt to authenticate using SASL (unknown error)
2015-06-01 13:24:32 INFO ClientCnxn:1083 - Client session timed out, have not heard from server in 1102ms for sessionid 0x0, closing socket connection and attempting reconnect
2015-06-01 13:24:33 INFO ClientCnxn:966 - Opening socket connection to server 192.168.56.103/192.168.56.103:2181. Will not attempt to authenticate using SASL (unknown error)
2015-06-01 13:24:33 INFO ClientCnxn:1083 - Client session timed out, have not heard from server in 1102ms for sessionid 0x0, closing socket connection and attempting reconnect
2015-06-01 13:24:34 INFO ClientCnxn:966 - Opening socket connection to server 192.168.56.103/192.168.56.103:2181. Will not attempt to authenticate using SASL (unknown error)
2015-06-01 13:24:34 INFO ClientCnxn:1083 - Client session timed out, have not heard from server in 1102ms for sessionid 0x0, closing socket connection and attempting reconnect
2015-06-01 13:24:35 INFO ClientCnxn:966 - Opening socket connection to server 192.168.56.103/192.168.56.103:2181. Will not attempt to authenticate using SASL (unknown error)
2015-06-01 13:24:36 INFO ClientCnxn:1083 - Client session timed out, have not heard from server in 1101ms for sessionid 0x0, closing socket connection and attempting reconnect
这一直在继续。如何在Tomcat中停止此行为?谢谢!