source: trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitorRemoteIdGenerator.java @ 1991

Last change on this file since 1991 was 1991, checked in by pharms, 9 years ago
  • added ordering id for requests and responses
File size: 4.4 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.httpmonitor.proxy;
16
17import java.util.logging.Level;
18
19import org.eclipse.jetty.client.api.Request;
20import org.eclipse.jetty.client.api.Result;
21import org.eclipse.jetty.client.api.Response.CompleteListener;
22import org.eclipse.jetty.http.HttpMethod;
23import org.eclipse.jetty.http.HttpVersion;
24
25import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent;
26import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException;
27import de.ugoe.cs.autoquest.httpmonitor.IdGenerator;
28import de.ugoe.cs.autoquest.httpmonitor.SimpleIdGenerator;
29import de.ugoe.cs.util.console.Console;
30
31/**
32 * <p>
33 * if a central monitoring server is used by the proxy, then this id generator is a connection to
34 * the server to receive centrally unique ordering ids from the central server. If the central
35 * server cannot be reached, this id generated uses a fallback internal {@link SimpleIdGenerator}.
36 * </p>
37 *
38 * @author Patrick Harms
39 */
40public class HttpMonitorRemoteIdGenerator
41    extends HttpMonitorRemoteConnection
42    implements CompleteListener, IdGenerator, HttpMonitorComponent
43{
44    /** the fall back id generator used internally if the server is not reachable*/
45    private IdGenerator fallBackIdGenerator = new SimpleIdGenerator();
46   
47    /**
48     * <p>
49     * initializes the id generator with the host and port of the central server from which to
50     * retrieve the ids
51     * </p>
52     *
53     * @param httpMonitorServer the host name of the central server
54     * @param httpMonitorPort   the port of the central server
55     */
56    public HttpMonitorRemoteIdGenerator(String httpMonitorServer, int httpMonitorPort) {
57        super(httpMonitorServer, httpMonitorPort);
58    }
59
60    /* (non-Javadoc)
61     * @see de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitorRemoteConnection#init()
62     */
63    @Override
64    public void init() throws IllegalStateException, HttpMonitorException {
65        fallBackIdGenerator.init();
66        super.init();
67    }
68
69    /* (non-Javadoc)
70     * @see de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitorRemoteConnection#start()
71     */
72    @Override
73    public void start() throws IllegalStateException, HttpMonitorException {
74        fallBackIdGenerator.start();
75        super.start();
76    }
77
78    /* (non-Javadoc)
79     * @see de.ugoe.cs.autoquest.httpmonitor.proxy.HttpMonitorRemoteConnection#stop()
80     */
81    @Override
82    public void stop() {
83        super.stop();
84        fallBackIdGenerator.stop();
85    }
86
87    /* (non-Javadoc)
88     * @see de.ugoe.cs.autoquest.httpmonitor.IdGenerator#getNextId()
89     */
90    @Override
91    public synchronized long getNextId() {
92        Request httpMonitorRequest = super.newRequest();
93        httpMonitorRequest.method(HttpMethod.GET);
94        httpMonitorRequest.version(HttpVersion.HTTP_1_1);
95        super.sendRequest(httpMonitorRequest);
96
97        Result result = super.getResult(httpMonitorRequest);
98       
99        if (result.isFailed()) {
100            Console.traceln
101                (Level.WARNING, "could not retrieve next id correctly: " + result.getFailure());
102            Console.logException((Exception) result.getFailure());
103           
104            Console.traceln(Level.WARNING, "using fallback, i.e., internal id generator");
105            return fallBackIdGenerator.getNextId();
106        }
107        else {
108            try {
109                return result.getResponse().getHeaders().getLongField("X-AutoQUEST-OrderingId");
110            }
111            catch (NumberFormatException e) {
112                Console.traceln(Level.WARNING, "could not retrieve next id correctly: " + e);
113                Console.logException(e);
114               
115                Console.traceln(Level.WARNING, "using fallback, i.e., internal id generator");
116                return fallBackIdGenerator.getNextId();
117            }
118        }
119       
120    }
121
122}
Note: See TracBrowser for help on using the repository browser.