Permalink
Browse files

Remove SessionClient and update Services and Events code to recognize…

… new

service.timeout and service.remove event.
  • Loading branch information...
Kami committed Mar 11, 2013
1 parent 7ff27b9 commit 1b921300ab23df8d424eecdc45326fbe4e156798
@@ -29,7 +29,6 @@
import org.apache.http.params.HttpParams;
public class Client {
- private final SessionsClient sessions;
private final ServicesClient services;
private final ConfigurationClient configuration;
private final ViewsClient views;
@@ -58,15 +57,13 @@ protected ClientConnectionManager createClientConnectionManager() {
}
}, username, apiKey, region);
- this.sessions = new SessionsClient(authClient);
this.services = new ServicesClient(authClient);
this.configuration = new ConfigurationClient(authClient);
this.views = new ViewsClient(authClient);
this.events = new EventsClient(authClient);
this.account = new AccountClient(authClient);
}
-
- public SessionsClient getSessionsClient() { return this.sessions; }
+
public ServicesClient getServicesClient() { return this.services; }
public ConfigurationClient getConfigurationClient() { return this.configuration; }
public ViewsClient getViewsClient() { return this.views; }
@@ -20,7 +20,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.rackspacecloud.client.service_registry.exceptions.ValidationException;
-import com.rackspacecloud.client.service_registry.objects.EventPayload;
+import com.rackspacecloud.client.service_registry.objects.Event;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -64,8 +64,7 @@ private Object processResponse() throws IOException, ValidationException {
if (this.parseAsJson && this.responseType != null) {
GsonBuilder builder = new GsonBuilder();
- Gson gson = builder.registerTypeAdapter(EventPayload.class,
- new EventPayload()).create();
+ Gson gson = builder.registerTypeAdapter(Event.class, new Event()).create();
data = gson.fromJson(data.toString(), this.responseType);
}
}
@@ -35,7 +35,7 @@
public class HeartBeater extends BaseClient {
private static final long INTERVAL_PROACTIVE_MILLIS = 4000L;
- private final String sessionId;
+ private final String serviceId;
private final Integer heartbeatTimeoutSecs;
private final long heartbeatIntervalMillis;
private final String initialToken;
@@ -45,17 +45,17 @@
private static final Logger logger = LoggerFactory.getLogger(HeartBeater.class);
- public HeartBeater(AuthClient authClient, String sessionId, String initialToken, int timeout) {
+ public HeartBeater(AuthClient authClient, String serviceId, String initialToken, int timeout) {
super(authClient);
- this.sessionId = sessionId;
+ this.serviceId = serviceId;
this.initialToken = initialToken;
this.heartbeatTimeoutSecs = timeout;
this.heartbeatIntervalMillis = (long)(timeout * 1000L * (timeout < 15 ? 0.6d : 0.9d));
}
private void runInThread() {
ClientResponse response;
- String path = String.format("/sessions/%s/heartbeat", this.sessionId);
+ String path = String.format("/services/%s/heartbeat", this.serviceId);
int lastHttpStatus = 0;
boolean isError = false;
String nextToken = initialToken;
@@ -118,4 +118,4 @@ public synchronized void stop() {
this.stopped = true;
hbThread.interrupt();
}
-}
+}
@@ -17,21 +17,21 @@
package com.rackspacecloud.client.service_registry;
-import com.rackspacecloud.client.service_registry.objects.Session;
+import com.rackspacecloud.client.service_registry.objects.Service;
-public class SessionCreateResponse {
- private final Session session;
+public class ServiceCreateResponse {
+ private final Service service;
private final String token;
private final HeartBeater heartbeater;
- public SessionCreateResponse(HeartBeater heartbeater, Session session, String token) {
- this.session = session;
+ public ServiceCreateResponse(HeartBeater heartbeater, Service service, String token) {
+ this.service = service;
this.token = token;
this.heartbeater = heartbeater;
}
- public Session getSession() {
- return session;
+ public Service getService() {
+ return service;
}
public String getToken() {
@@ -34,7 +34,8 @@
public class EventsClient extends BaseClient {
private static final List VALID_EVENT_TYPES = new ArrayList<String>(Arrays.asList(new String[]{
"service.join",
- "services.timeout",
+ "service.timeout",
+ "service.remove",
"configuration_value.update",
"configuration_value.remove"}));
@@ -82,13 +83,17 @@ public EventsClient(AuthClient authClient) {
event = new ServiceJoinEvent(eventPayload.getService());
result.add(event);
}
- else if (type.compareTo("services.timeout") == 0) {
- ServicesTimeoutEventPayload eventPayload = ((ServicesTimeoutEventPayload)payload);
+ else if (type.compareTo("service.timeout") == 0) {
+ ServiceTimeoutEventPayload eventPayload = ((ServiceTimeoutEventPayload)payload);
- for (Service service : eventPayload.getServices()) {
- event = new ServiceTimeoutEvent(service);
- result.add(event);
- }
+ event = new ServiceTimeoutEvent(eventPayload.getService());
+ result.add(event);
+ }
+ else if (type.compareTo("service.remove") == 0) {
+ ServiceRemoveEventPayload eventPayload = ((ServiceRemoveEventPayload)payload);
+
+ event = new ServiceRemoveEvent(eventPayload.getService());
+ result.add(event);
}
else if (type.compareTo("configuration_value.update") == 0) {
ConfigurationValueUpdatedEventPayload eventPayload = ((ConfigurationValueUpdatedEventPayload)payload);
@@ -19,8 +19,11 @@
import com.google.gson.reflect.TypeToken;
import com.rackspacecloud.client.service_registry.ClientResponse;
+import com.rackspacecloud.client.service_registry.HeartBeater;
import com.rackspacecloud.client.service_registry.PaginationOptions;
+import com.rackspacecloud.client.service_registry.ServiceCreateResponse;
import com.rackspacecloud.client.service_registry.containers.ServicesContainer;
+import com.rackspacecloud.client.service_registry.objects.HeartbeatToken;
import com.rackspacecloud.client.service_registry.objects.Service;
import com.rackspacecloud.client.service_registry.objects.Session;
import org.apache.http.NameValuePair;
@@ -36,8 +39,11 @@
import java.util.Map;
public class ServicesClient extends BaseClient {
+ private final AuthClient authClient;
+
public ServicesClient(AuthClient authClient) {
super(authClient);
+ this.authClient = authClient;
}
public List<Service> list(PaginationOptions paginationOptions) throws Exception {
@@ -64,11 +70,13 @@ public Service get(String id) throws Exception {
return (Service)response.getBody();
}
- public Service create(String id, String sessionId, List<String> tags, Map<String, String> metadata) throws Exception {
- Service service = new Service(id, sessionId, tags, metadata);
- ClientResponse response = this.performRequestWithPayload("/services", null, new HttpPost(), service, false, null);
+ public ServiceCreateResponse create(String id, int heartbeatTimeout, List<String> tags, Map<String, String> metadata) throws Exception {
+ Service service = new Service(id, heartbeatTimeout, tags, metadata);
+ ClientResponse response = this.performRequestWithPayload("/services", null, new HttpPost(), service, true, HeartbeatToken.class);
+
+ HeartbeatToken hbt = (HeartbeatToken)response.getBody(); HeartBeater heartBeater = new HeartBeater(this.authClient, id, hbt.getToken(), service.getHeartbeatTimeout());
- return new Service(id, sessionId, tags, metadata);
+ return new ServiceCreateResponse(heartBeater, service, hbt.getToken());
}
public ServicesClient update(String id, List<String> tags, Map<String, String> metadata) throws Exception {
@@ -1,76 +0,0 @@
-/**
- * Copyright 2012 Rackspace
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package com.rackspacecloud.client.service_registry.clients;
-
-import com.google.gson.reflect.TypeToken;
-import com.rackspacecloud.client.service_registry.*;
-import com.rackspacecloud.client.service_registry.containers.SessionsContainer;
-import com.rackspacecloud.client.service_registry.objects.HeartbeatToken;
-import com.rackspacecloud.client.service_registry.objects.Session;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
-
-import java.lang.reflect.Type;
-import java.util.List;
-import java.util.Map;
-
-public class SessionsClient extends BaseClient {
- private final AuthClient authClient;
-
- public SessionsClient(AuthClient authClient) {
- super(authClient);
- this.authClient = authClient;
- }
-
- public List<Session> list(PaginationOptions paginationOptions) throws Exception {
- Type type = new TypeToken<SessionsContainer>() {}.getType();
- ClientResponse response = this.performListRequest(paginationOptions, "/sessions", null, new HttpGet(), true, type);
-
- SessionsContainer container = (SessionsContainer)response.getBody();
- return container.getValues();
- }
-
- public Session get(String id) throws Exception {
- ClientResponse response = this.performRequest("/sessions/" + id, null, new HttpGet(), true, Session.class);
- return (Session)response.getBody();
- }
-
- public SessionCreateResponse create(int heartbeatTimeout, Map<String, String> metadata) throws Exception {
- Session session = new Session(null, heartbeatTimeout, null, metadata);
- ClientResponse response = this.performRequestWithPayload("/sessions", null, new HttpPost(), session, true, HeartbeatToken.class);
-
- String id = Utils.getIdFromLocationHeader(response.getHeader("Location")[0].getValue());
- HeartbeatToken hbt = (HeartbeatToken)response.getBody();
-
- HeartBeater heartBeater = new HeartBeater(this.authClient, session.getId(), hbt.getToken(), session.getHeartbeatTimeout());
- return new SessionCreateResponse(heartBeater, new Session(id, heartbeatTimeout, null, metadata), hbt.getToken());
- }
-
- public SessionsClient update(String id, int heartbeatTimeout, Map<String, String> metadata) throws Exception {
- Session session = new Session(null, heartbeatTimeout, null, metadata);
- ClientResponse response = this.performRequestWithPayload("/sessions/" + id, null, new HttpPut(), session, true, null);
- return this;
- }
-
- public String heartbeat(String id, String token) throws Exception {
- HeartbeatToken ht = new HeartbeatToken(token);
- ClientResponse response = this.performRequestWithPayload("/sessions/" + id + "/heartbeat", null, new HttpPost(), ht, true, HeartbeatToken.class);
- return ((HeartbeatToken)response.getBody()).getToken();
- }
-}
@@ -0,0 +1,20 @@
+package com.rackspacecloud.client.service_registry.events.server;
+
+import com.rackspacecloud.client.service_registry.objects.Service;
+
+public class ServiceRemoveEvent extends BaseEvent {
+ private Service service;
+
+ public ServiceRemoveEvent(Service service) {
+ this.service = service;
+ }
+
+ public Service getService() {
+ return service;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("[ServiceRemoveEvent id=%s]", this.service.getId());
+ }
+}
@@ -17,15 +17,23 @@
package com.rackspacecloud.client.service_registry.objects;
+import com.google.gson.*;
+import com.google.gson.reflect.TypeToken;
+
+import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
-public class Event {
+public class Event implements
+ InstanceCreator<Event>,
+ JsonDeserializer<Event> {
private String id;
private Long timestamp;
private String type;
private EventPayload payload;
+ public Event() {}
+
public Event(String id, Long timestamp, String type, EventPayload payload) {
this.id = id;
this.timestamp = timestamp;
@@ -48,4 +56,70 @@ public String getType() {
public EventPayload getPayload() {
return payload;
}
-}
+
+ @Override
+ public Event createInstance(Type type) {
+ return this;
+ }
+
+ @Override
+ public Event deserialize(JsonElement json, Type typeOfT,
+ JsonDeserializationContext context) {
+ JsonObject jsonObject = json.getAsJsonObject();
+
+ String id = jsonObject.get("id").getAsString();
+ String eventType = jsonObject.get("type").getAsString();
+ JsonObject payloadJsonObject = jsonObject.get("payload").getAsJsonObject();
+ Long timestamp = jsonObject.get("timestamp").getAsLong();
+
+ System.out.print("===");
+ System.out.println(eventType);
+
+ EventPayload payload = null;
+
+ if (eventType.equals("configuration_value.update") ||
+ eventType.equals("configuration_value.remove")) {
+ String configurationId = payloadJsonObject.get("configuration_value_id").getAsString();
+ String oldValue, newValue;
+
+ if (payloadJsonObject.get("old_value").isJsonNull()) {
+ oldValue = null;
+ }
+ else {
+ oldValue = payloadJsonObject.get("old_value").getAsString();
+ }
+
+ if (payloadJsonObject.has("new_value")) {
+ newValue = payloadJsonObject.get("new_value").getAsString();
+
+ payload = new ConfigurationValueUpdatedEventPayload(configurationId, oldValue, newValue);
+ }
+ else {
+ payload = new ConfigurationValueRemovedEventPayload(configurationId, oldValue);
+ }
+
+ System.out.println(payload);
+
+ }
+ else if (eventType.equals("service.join") || eventType.equals("service.timeout") ||
+ eventType.equals("service.remove")) {
+ Service service = new Gson().fromJson(jsonObject,
+ new TypeToken<Service>() {}.getType());
+
+ if (eventType.equals("service.join")) {
+ payload = new ServiceJoinEventPayload(service);
+ }
+ else if (eventType.equals("service.timeout")) {
+ payload = new ServiceTimeoutEventPayload(service);
+ }
+ else if (eventType.equals("service.remove")) {
+ payload = new ServiceRemoveEventPayload(service);
+ }
+ }
+ else {
+ throw new Error("Unrecognized event type: " + eventType);
+ }
+
+ return new Event(id, timestamp, eventType, payload);
+ }
+}
Oops, something went wrong.

0 comments on commit 1b92130

Please sign in to comment.