-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Microprofile BOM/POM #41
Changes from all commits
1dc19af
cae51eb
b8ef867
98f9bae
d0061df
652a50a
22bb28d
9172e52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2017 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* 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 org.eclipse.microprofile.sample.canonical.rest; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.eclipse.microprofile.sample.canonical.utils.QLogger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.logging.Logger; | ||
|
||
@Path("/health") | ||
@RequestScoped | ||
public class HealthEndpoint { | ||
|
||
@Inject | ||
@QLogger | ||
private Logger logger; | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public String getHealthStatus() { | ||
final String NAME = "CDs"; | ||
HealthCheckResponse response; | ||
final List<Integer> randomCDs = getRandomNumbers(); | ||
if (randomCDs.size() > 0) { | ||
response = HealthCheckResponse.named(NAME).up().build(); | ||
} else { | ||
response = HealthCheckResponse.named(NAME).down().build(); | ||
} | ||
return response.toString(); | ||
} | ||
|
||
private List<Integer> getRandomNumbers() { | ||
final List<Integer> randomCDs = new ArrayList<>(); | ||
final Random r = new Random(); | ||
for (int i=0; i < r.nextInt(); i++) { | ||
randomCDs.add(i + 1101); | ||
} | ||
|
||
logger.info("Top CDs are " + randomCDs); | ||
|
||
return randomCDs; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright (c) 2017 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* 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 org.eclipse.microprofile.sample.canonical.utils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.eclipse.microprofile.health.HealthCheckResponseBuilder; | ||
import org.eclipse.microprofile.health.spi.HealthCheckResponseProvider; | ||
|
||
/** | ||
* @author Werner Keil | ||
*/ | ||
public class CanonicalHealthCheckResponseProvider implements HealthCheckResponseProvider{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the app does not implement the spi but use application server's implementation, e.g. Open Liberty etc. Application should only provide the HealthCheckResponse. One of the benefits of MP programming model is to make app development a lot easier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
@Override | ||
public HealthCheckResponseBuilder createResponseBuilder() { | ||
return new ResponseBuilder(); | ||
} | ||
|
||
private static class ResponseBuilder extends HealthCheckResponseBuilder { | ||
private String name; | ||
private boolean state; | ||
private Map<String, Object> data = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public HealthCheckResponseBuilder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseBuilder withData(String key, String value) { | ||
data.put(key, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseBuilder withData(String key, long value) { | ||
data.put(key, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseBuilder withData(String key, boolean value) { | ||
data.put(key, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseBuilder up() { | ||
return state(true); | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseBuilder down() { | ||
return state(false); | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseBuilder state(boolean up) { | ||
this.state = up; | ||
return this; | ||
} | ||
|
||
@Override | ||
public HealthCheckResponse build() { | ||
return new Response(this.name, this.state, this.data); | ||
} | ||
|
||
private class Response extends HealthCheckResponse { | ||
private final String name; | ||
private final boolean up; | ||
private final Map<String, Object> data; | ||
|
||
public Response(String name, boolean up, Map<String, Object> map) { | ||
this.name = name; | ||
this.up = up; | ||
this.data = setData(map); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Response [" + name + "=" + getState() + | ||
(data!= null ? ", data=" + data : "") + "]"; | ||
} | ||
|
||
private Map<String,Object> setData(Map<String, Object> map) { | ||
if (map.isEmpty()) { | ||
return null; | ||
} | ||
Map<String, Object> data = new HashMap<>(); | ||
for (String key : map.keySet()) { | ||
final Object modelValue = map.get(key); | ||
final Object value; | ||
if (modelValue instanceof Long) { | ||
value = ((Long)modelValue).longValue(); | ||
} else { | ||
value = modelValue; | ||
} | ||
data.put(key, value); | ||
} | ||
return data; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public State getState() { | ||
return up ? State.UP : State.DOWN; | ||
} | ||
|
||
@Override | ||
public Optional<Map<String, Object>> getData() { | ||
return Optional.ofNullable(data); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.eclipse.microprofile.sample.canonical.utils.CanonicalHealthCheckResponseProvider |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want this either according to what @Emily-Jiang wrote, but rather just provide an
@Health
method as e.g. shown in https://github.com/pilhuhn/microprofile-demo/blob/master/src/main/java/de/bsd/microprofiledemo/rest/HealthDemo.java#L29-L37There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above example looks vendor-neutral as opposed to what @Emily-Jiang suggested sounded a bit more like abandoning a vendor-neutral Eclipse example altogether in favor of examples by OpenLiberty, Wildfly, Kumuluz, etc. ?;-O