7new Socket(address, port).getOutputStream());
8} catch (IOException e) {
9if (e instanceof InterruptedIOException) {
10Thread.currentThread().interrupt();
11}
12String msg = "Could not connect to remote log4j server at ["
13+ address.getHostName() + "].";
14if (reconnectionDelay > 0) {
15msg += " We will try again later.";
16fireConnector(); // fire the connector thread
17} else {
18msg += " We are not retrying.";
19errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE);
20}
21LogLog.error(msg);
22}
23 }
如果创建失败,调用fireConnector()方法,创建一个Connector线程,在每间隔reconnectionDelay(默认值为30000ms,若将其设置为0表示在链接出问题时不创建新的线程检测)的时间里不断重试链接。当链接重新建立后,Connector线程退出并将connector实例置为null以在下一次链接出现问题时创建的Connector线程检测。
1 实例置为null以在下一次链接出现问题时创建的Connector线程检测。
2 void fireConnector() {
3if (connector == null) {
4LogLog.debug("Starting a new connector thread.");
5connector = new Connector();
6connector.setDaemon(true);
7connector.setPriority(Thread.MIN_PRIORITY);
8connector.start();
9}
10 }
11
12 class Connector extends Thread {
13boolean interrupted = false;
14public void run() {
15Socket socket;
16while (!interrupted) {
17try {
18sleep(reconnectionDelay);
19LogLog.debug("Attempting connection to "
20+ address.getHostName());
21socket = new Socket(address, port);
22synchronized (this) {
23oos = new ObjectOutputStream(socket.getOutputStream());
24connector = null;
25LogLog.debug("Connection established. Exiting connector thread.");
26break;
27}
28} catch (InterruptedException e) {
29LogLog.debug("Connector interrupted. Leaving loop.");
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-29497-17.html
我昨天才在超市挑选黑芝麻