Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove SessionClient and update Services and Events code to recognize…

… new

service.timeout and service.remove event.
  • Loading branch information...
commit 1b921300ab23df8d424eecdc45326fbe4e156798 1 parent 7ff27b9
@Kami Kami authored
Showing with 195 additions and 177 deletions.
  1. +1 −4 src/main/java/com/rackspacecloud/client/service_registry/Client.java
  2. +2 −3 src/main/java/com/rackspacecloud/client/service_registry/ClientResponse.java
  3. +5 −5 src/main/java/com/rackspacecloud/client/service_registry/HeartBeater.java
  4. +7 −7 ...a/com/rackspacecloud/client/service_registry/{SessionCreateResponse.java → ServiceCreateResponse.java}
  5. +12 −7 src/main/java/com/rackspacecloud/client/service_registry/clients/EventsClient.java
  6. +12 −4 src/main/java/com/rackspacecloud/client/service_registry/clients/ServicesClient.java
  7. +0 −76 src/main/java/com/rackspacecloud/client/service_registry/clients/SessionsClient.java
  8. +20 −0 src/main/java/com/rackspacecloud/client/service_registry/events/server/ServiceRemoveEvent.java
  9. +76 −2 src/main/java/com/rackspacecloud/client/service_registry/objects/Event.java
  10. +2 −55 src/main/java/com/rackspacecloud/client/service_registry/objects/EventPayload.java
  11. +19 −6 src/main/java/com/rackspacecloud/client/service_registry/objects/Service.java
  12. +6 −7 ...ud/client/service_registry/objects/{ServicesTimeoutEventPayload.java → ServiceRemoveEventPayload.java}
  13. +31 −0 src/main/java/com/rackspacecloud/client/service_registry/objects/ServiceTimeoutEventPayload.java
  14. +2 −1  src/main/java/com/rackspacecloud/client/service_registry/objects/Session.java
View
5 src/main/java/com/rackspacecloud/client/service_registry/Client.java
@@ -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; }
View
5 src/main/java/com/rackspacecloud/client/service_registry/ClientResponse.java
@@ -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);
}
}
View
10 src/main/java/com/rackspacecloud/client/service_registry/HeartBeater.java
@@ -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,9 +45,9 @@
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));
@@ -55,7 +55,7 @@ public HeartBeater(AuthClient authClient, String sessionId, String initialToken,
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();
}
-}
+}
View
14 ...lient/service_registry/SessionCreateResponse.java → ...lient/service_registry/ServiceCreateResponse.java
@@ -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() {
View
19 src/main/java/com/rackspacecloud/client/service_registry/clients/EventsClient.java
@@ -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);
View
16 src/main/java/com/rackspacecloud/client/service_registry/clients/ServicesClient.java
@@ -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 {
View
76 src/main/java/com/rackspacecloud/client/service_registry/clients/SessionsClient.java
@@ -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();
- }
-}
View
20 src/main/java/com/rackspacecloud/client/service_registry/events/server/ServiceRemoveEvent.java
@@ -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());
+ }
+}
View
78 src/main/java/com/rackspacecloud/client/service_registry/objects/Event.java
@@ -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);
+ }
+}
View
57 src/main/java/com/rackspacecloud/client/service_registry/objects/EventPayload.java
@@ -22,59 +22,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-public class EventPayload implements
- InstanceCreator<EventPayload>,
- JsonDeserializer<EventPayload> {
-
- @Override
- public EventPayload createInstance(Type type) {
- return this;
- }
-
- @Override
- public EventPayload deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
- // TODO: This is nasty - propagate type here
- if (json.getClass().equals(JsonObject.class)) {
- JsonObject jsonObject = json.getAsJsonObject();
-
- if (jsonObject.has("configuration_value_id")) {
- // configuration_value.update, configuration_value.remove
- String id = jsonObject.get("configuration_value_id").getAsString();
- String oldValue, newValue;
-
- if (jsonObject.get("old_value").isJsonNull()) {
- oldValue = null;
- }
- else {
- oldValue = jsonObject.get("old_value").getAsString();
- }
-
- if (jsonObject.has("new_value")) {
- newValue = jsonObject.get("new_value").getAsString();
-
- return new ConfigurationValueUpdatedEventPayload(id, oldValue, newValue);
- }
- else {
- return new ConfigurationValueRemovedEventPayload(id, oldValue);
- }
- }
- else {
- // service.join
- Service service = new Gson().fromJson(jsonObject, new TypeToken<Service>() {}.getType());
- return new ServiceJoinEventPayload(service);
- }
- }
- else if (json.getClass().equals(JsonArray.class)) {
- // services.timeout
- JsonArray jsonArray = json.getAsJsonArray();
- ArrayList<Service> services = new Gson().fromJson(jsonArray, new TypeToken<ArrayList<Service>>() {}.getType());
-
- return new ServicesTimeoutEventPayload(services);
- }
-
- return null;
- }
-}
+public class EventPayload {
+}
View
25 src/main/java/com/rackspacecloud/client/service_registry/objects/Service.java
@@ -27,14 +27,23 @@
public class Service {
private String id;
- @SerializedName("session_id")
- private String sessionId;
+ @SerializedName("heartbeat_timeout")
+ private Integer heartbeatTimeout;
+ private Integer lastSeen = null;
+
private List<String> tags = new ArrayList<String>();
private Map<String, String> metadata = new HashMap<String, String>();
- public Service(String id, String sessionId, List<String> tags, Map<String, String> metadata) {
+ public Service(String id, Integer heartbeatTimeout, List<String> tags,
+ Map<String, String> metadata) {
+ this(id, heartbeatTimeout, null, tags, metadata);
+ }
+
+ public Service(String id, Integer heartbeatTimeout, Integer lastSeen,
+ List<String> tags, Map<String, String> metadata) {
this.id = id;
- this.sessionId = sessionId;
+ this.heartbeatTimeout = heartbeatTimeout;
+ this.lastSeen = lastSeen;
this.tags = tags;
this.metadata = metadata;
}
@@ -43,8 +52,12 @@ public String getId() {
return id;
}
- public String getSessionId() {
- return sessionId;
+ public Integer getHeartbeatTimeout() {
+ return heartbeatTimeout;
+ }
+
+ private Integer getLastSeen() {
+ return lastSeen;
}
public List<String> getTags() {
View
13 ...registry/objects/ServicesTimeoutEventPayload.java → ...e_registry/objects/ServiceRemoveEventPayload.java
@@ -17,16 +17,15 @@
package com.rackspacecloud.client.service_registry.objects;
-import java.util.List;
-public class ServicesTimeoutEventPayload extends EventPayload {
- private List<Service> services;
+public class ServiceRemoveEventPayload extends EventPayload {
+ private Service service;
- public ServicesTimeoutEventPayload(List<Service> services) {
- this.services = services;
+ public ServiceRemoveEventPayload(Service service) {
+ this.service = service;
}
- public List<Service> getServices() {
- return services;
+ public Service getService() {
+ return service;
}
}
View
31 src/main/java/com/rackspacecloud/client/service_registry/objects/ServiceTimeoutEventPayload.java
@@ -0,0 +1,31 @@
+/**
+ * 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.objects;
+
+
+public class ServiceTimeoutEventPayload extends EventPayload {
+ private Service service;
+
+ public ServiceTimeoutEventPayload(Service service) {
+ this.service = service;
+ }
+
+ public Service getService() {
+ return service;
+ }
+}
View
3  src/main/java/com/rackspacecloud/client/service_registry/objects/Session.java
@@ -28,7 +28,8 @@
private Integer heartbeatTimeout;
private Map<String, String> metadata;
- public Session(String id, Integer heartbeatTimeout, Integer lastSeen, Map<String, String> metadata) {
+ public Session(String id, Integer heartbeatTimeout, Integer lastSeen,
+ Map<String, String> metadata) {
this.id = id;
this.heartbeatTimeout = heartbeatTimeout;
this.lastSeen = lastSeen;
Please sign in to comment.
Something went wrong with that request. Please try again.