Browse Source

Move Request and Response to loadbalancer package. Fixes gh-772. (#773)

* Move Request and Response to loadbalancer package. Fixes gh-772.

* Fix checkstyle.

* Inherit and reference repackaged classes.

* Revert "Inherit and reference repackaged classes."

This reverts commit 02808c5f18.

* Fix Status enums.

* Make old classes extend the new ones.
pull/789/head
Olga Maciaszek-Sharma 4 years ago committed by GitHub
parent
commit
4eb9dd0f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java
  2. 46
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java
  3. 47
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java
  4. 47
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java
  5. 41
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/EmptyResponse.java
  6. 34
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Request.java
  7. 40
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Response.java
  8. 32
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/CompletionContext.java
  9. 19
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequest.java
  10. 22
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequestContext.java
  11. 21
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultResponse.java
  12. 17
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/EmptyResponse.java
  13. 12
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Request.java
  14. 9
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Response.java

70
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/CompletionContext.java

@ -0,0 +1,70 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
import org.springframework.core.style.ToStringCreator;
/**
* @author Spencer Gibb
*/
// TODO: add metrics
public class CompletionContext {
private final Status status;
private final Throwable throwable;
public CompletionContext(Status status) {
this(status, null);
}
public CompletionContext(Status status, Throwable throwable) {
this.status = status;
this.throwable = throwable;
}
public Status status() {
return this.status;
}
public Throwable getThrowable() {
return this.throwable;
}
@Override
public String toString() {
ToStringCreator to = new ToStringCreator(this);
to.append("status", this.status);
to.append("throwable", this.throwable);
return to.toString();
}
/**
* Request status state.
*/
public enum Status {
/** Request was handled successfully. */
SUCCESS,
/** Request reached the server but failed due to timeout or internal error. */
FAILED,
/** Request did not go off box and should not be counted for statistics. */
DISCARD,
}
}

46
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequest.java

@ -0,0 +1,46 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
/**
* A default implementation of {@link Request}.
*
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
*/
public class DefaultRequest<T> implements Request<T> {
private T context;
public DefaultRequest() {
new DefaultRequestContext();
}
public DefaultRequest(T context) {
this.context = context;
}
@Override
public T getContext() {
return context;
}
public void setContext(T context) {
this.context = context;
}
}

47
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultRequestContext.java

@ -0,0 +1,47 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
/**
* Contains information relevant to the request.
*
* @author Olga Maciaszek-Sharma
*/
public class DefaultRequestContext {
/**
* A {@link String} value of hint that can be used to choose the correct service
* instance.
*/
private String hint = "default";
public DefaultRequestContext() {
}
public DefaultRequestContext(String hint) {
this.hint = hint;
}
public String getHint() {
return hint;
}
public void setHint(String hint) {
this.hint = hint;
}
}

47
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/DefaultResponse.java

@ -0,0 +1,47 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
import org.springframework.cloud.client.ServiceInstance;
/**
* @author Spencer Gibb
*/
public class DefaultResponse implements Response<ServiceInstance> {
private final ServiceInstance serviceInstance;
public DefaultResponse(ServiceInstance serviceInstance) {
this.serviceInstance = serviceInstance;
}
@Override
public boolean hasServer() {
return this.serviceInstance != null;
}
@Override
public ServiceInstance getServer() {
return this.serviceInstance;
}
@Override
public void onComplete(CompletionContext completionContext) {
// TODO: implement
}
}

41
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/EmptyResponse.java

@ -0,0 +1,41 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
import org.springframework.cloud.client.ServiceInstance;
/**
* @author Spencer Gibb
*/
public class EmptyResponse implements Response<ServiceInstance> {
@Override
public boolean hasServer() {
return false;
}
@Override
public ServiceInstance getServer() {
return null;
}
@Override
public void onComplete(CompletionContext completionContext) {
// TODO: implement
}
}

34
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Request.java

@ -0,0 +1,34 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
/**
* Marker interface for a request.
*
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
*/
public interface Request<C> {
// Avoid breaking backward compatibility
default C getContext() {
return null;
}
// TODO: define contents
}

40
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/Response.java

@ -0,0 +1,40 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.client.loadbalancer;
/**
* Response created for each request.
*
* @param <T> type of the server
* @author Spencer Gibb
*/
public interface Response<T> {
boolean hasServer();
T getServer();
/**
* Notification that the request completed.
* @deprecated <code>onComplete</code> callbacks for load-balanced calls are being
* moved to a separate interface
* @param completionContext - completion context
*/
@Deprecated
void onComplete(CompletionContext completionContext);
}

32
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/CompletionContext.java

@ -16,46 +16,46 @@
package org.springframework.cloud.client.loadbalancer.reactive; package org.springframework.cloud.client.loadbalancer.reactive;
import org.springframework.core.style.ToStringCreator;
/** /**
* @deprecated in favour of
* {@link org.springframework.cloud.client.loadbalancer.CompletionContext}
* @author Spencer Gibb * @author Spencer Gibb
*/ */
// TODO: add metrics // TODO: add metrics
public class CompletionContext { @Deprecated
public class CompletionContext
extends org.springframework.cloud.client.loadbalancer.CompletionContext {
private final Status status; private final Status status;
private final Throwable throwable;
public CompletionContext(Status status) { public CompletionContext(Status status) {
this(status, null); this(status, null);
} }
public CompletionContext(Status status, Throwable throwable) { public CompletionContext(Status status, Throwable throwable) {
super(resolveStatus(status), throwable);
this.status = status; this.status = status;
this.throwable = throwable;
} }
public Status getStatus() { public Status getStatus() {
return this.status; return this.status;
} }
public Throwable getThrowable() { private static org.springframework.cloud.client.loadbalancer.CompletionContext.Status resolveStatus(
return this.throwable; Status status) {
} if (Status.SUCCESSS.equals(status)) {
return org.springframework.cloud.client.loadbalancer.CompletionContext.Status.SUCCESS;
@Override }
public String toString() { return org.springframework.cloud.client.loadbalancer.CompletionContext.Status
ToStringCreator to = new ToStringCreator(this); .valueOf(status.name());
to.append("status", this.status);
to.append("throwable", this.throwable);
return to.toString();
} }
/** /**
* Request status state. * Request status state.
* @deprecated in favour of
* {@link org.springframework.cloud.client.loadbalancer.CompletionContext.Status}
*/ */
@Deprecated
public enum Status { public enum Status {
/** Request was handled successfully. */ /** Request was handled successfully. */

19
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequest.java

@ -19,28 +19,21 @@ package org.springframework.cloud.client.loadbalancer.reactive;
/** /**
* A default implementation of {@link Request}. * A default implementation of {@link Request}.
* *
* @deprecated in favour of
* @author Spencer Gibb * @author Spencer Gibb
* @author Olga Maciaszek-Sharma * @author Olga Maciaszek-Sharma
*/ */
public class DefaultRequest<T> implements Request<T> { @Deprecated
public class DefaultRequest<T>
private T context; extends org.springframework.cloud.client.loadbalancer.DefaultRequest<T>
implements Request<T> {
public DefaultRequest() { public DefaultRequest() {
new DefaultRequestContext(); new DefaultRequestContext();
} }
public DefaultRequest(T context) { public DefaultRequest(T context) {
this.context = context; super(context);
}
@Override
public T getContext() {
return context;
}
public void setContext(T context) {
this.context = context;
} }
} }

22
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultRequestContext.java

@ -19,29 +19,19 @@ package org.springframework.cloud.client.loadbalancer.reactive;
/** /**
* Contains information relevant to the request. * Contains information relevant to the request.
* *
* @deprecated in favour of
* {@link org.springframework.cloud.client.loadbalancer.DefaultRequestContext}
* @author Olga Maciaszek-Sharma * @author Olga Maciaszek-Sharma
*/ */
public class DefaultRequestContext { @Deprecated
public class DefaultRequestContext
/** extends org.springframework.cloud.client.loadbalancer.DefaultRequestContext {
* A {@link String} value of hint that can be used to choose the correct service
* instance.
*/
private String hint = "default";
public DefaultRequestContext() { public DefaultRequestContext() {
} }
public DefaultRequestContext(String hint) { public DefaultRequestContext(String hint) {
this.hint = hint; super(hint);
}
public String getHint() {
return hint;
}
public void setHint(String hint) {
this.hint = hint;
} }
} }

21
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DefaultResponse.java

@ -19,24 +19,17 @@ package org.springframework.cloud.client.loadbalancer.reactive;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
/** /**
* @deprecated in favour of
* {@link org.springframework.cloud.client.loadbalancer.DefaultResponse}
* @author Spencer Gibb * @author Spencer Gibb
*/ */
public class DefaultResponse implements Response<ServiceInstance> { @Deprecated
public class DefaultResponse
private final ServiceInstance serviceInstance; extends org.springframework.cloud.client.loadbalancer.DefaultResponse
implements Response<ServiceInstance> {
public DefaultResponse(ServiceInstance serviceInstance) { public DefaultResponse(ServiceInstance serviceInstance) {
this.serviceInstance = serviceInstance; super(serviceInstance);
}
@Override
public boolean hasServer() {
return this.serviceInstance != null;
}
@Override
public ServiceInstance getServer() {
return this.serviceInstance;
} }
@Override @Override

17
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/EmptyResponse.java

@ -19,19 +19,14 @@ package org.springframework.cloud.client.loadbalancer.reactive;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
/** /**
* @deprecated in favour of
* {@link org.springframework.cloud.client.loadbalancer.EmptyResponse}
* @author Spencer Gibb * @author Spencer Gibb
*/ */
public class EmptyResponse implements Response<ServiceInstance> { @Deprecated
public class EmptyResponse
@Override extends org.springframework.cloud.client.loadbalancer.EmptyResponse
public boolean hasServer() { implements Response<ServiceInstance> {
return false;
}
@Override
public ServiceInstance getServer() {
return null;
}
@Override @Override
public void onComplete(CompletionContext completionContext) { public void onComplete(CompletionContext completionContext) {

12
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Request.java

@ -19,16 +19,12 @@ package org.springframework.cloud.client.loadbalancer.reactive;
/** /**
* Marker interface for a request. * Marker interface for a request.
* *
* @deprecated in favour of {@link org.springframework.cloud.client.loadbalancer.Request}
* @author Spencer Gibb * @author Spencer Gibb
* @author Olga Maciaszek-Sharma * @author Olga Maciaszek-Sharma
*/ */
public interface Request<C> { @Deprecated
public interface Request<C>
// Avoid breaking backward compatibility extends org.springframework.cloud.client.loadbalancer.Request<C> {
default C getContext() {
return null;
}
// TODO: define contents
} }

9
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/Response.java

@ -19,14 +19,13 @@ package org.springframework.cloud.client.loadbalancer.reactive;
/** /**
* Response created for each request. * Response created for each request.
* *
* @deprecated in favour of {@link org.springframework.cloud.client.loadbalancer.Response}
* @param <T> type of the server * @param <T> type of the server
* @author Spencer Gibb * @author Spencer Gibb
*/ */
public interface Response<T> { @Deprecated
public interface Response<T>
boolean hasServer(); extends org.springframework.cloud.client.loadbalancer.Response<T> {
T getServer();
/** /**
* Notification that the request completed. * Notification that the request completed.

Loading…
Cancel
Save