Repository: tomee Updated Branches: refs/heads/master 708f8aec0 -> 21dfa9a72
Add readme file Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/21dfa9a7 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/21dfa9a7 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/21dfa9a7 Branch: refs/heads/master Commit: 21dfa9a72461047198cca66030af8be3d1bec38c Parents: 708f8ae Author: Jean-Louis Monteiro <[email protected]> Authored: Fri Dec 28 10:36:47 2018 +0100 Committer: Jean-Louis Monteiro <[email protected]> Committed: Fri Dec 28 10:36:47 2018 +0100 ---------------------------------------------------------------------- examples/cdi-session-scope/README.adoc | 134 +++++++++++++++++++++++++++ examples/mp-metrics-metered/README.adoc | 133 ++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/21dfa9a7/examples/cdi-session-scope/README.adoc ---------------------------------------------------------------------- diff --git a/examples/cdi-session-scope/README.adoc b/examples/cdi-session-scope/README.adoc new file mode 100644 index 0000000..e529424 --- /dev/null +++ b/examples/cdi-session-scope/README.adoc @@ -0,0 +1,134 @@ +index-group=Unrevised +type=page +status=unpublished + += CDI @SessionScoped + +This example show the use of `@SessionScoped` annotation for injected objects. An object +which is defined as `@SessionScoped` is created once for every HTTPSession and is shared by all the +beans that inject it throughout the same HTTPSession. + +== Run the application: + + mvn clean install tomee:run + +== Example + +This example has an end point wherein a user provides a request parameter 'name' which is persisted as a feild in a session scoped bean SessionBean and +then retrieved through another endpoint. + +== Request + +GET http://localhost:8080/cdi-session-scope/set-name?name=Puneeth + +== Response + +done, go to /name servlet + +== Request + +GET http://localhost:8080/cdi-session-scope/name + +== Response + +name = {Puneeth} + +== SessionBean + +The annotation @SessionScoped specifies that a bean is session scoped ie there will be only one instance of the class associated with a particular HTTPSession. + +[source,java] +---- +@SessionScoped +public class SessionBean implements Serializable { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} +---- + +== InputServlet + +InputServlet is a generic servlet which is mapped to the url pattern '/set-name'. +The session scoped bean 'SessionBean' has been injected into this servlet, and the incoming request parameter is set to the feild name of the bean. + +[source,java] +---- +@WebServlet(name = "input-servlet", urlPatterns = {"/set-name"}) +public class InputServlet extends HttpServlet { + + @Inject + private SessionBean bean; + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp + throws ServletException, IOException { + final String name = req.getParameter("name"); + if (name == null || name.isEmpty()) { + resp.getWriter().write("please add a parameter name=xxx"); + } else { + bean.setName(name); + resp.getWriter().write("done, go to /name servlet"); + } + + } +} +---- + +== AnswerBean + +AnswerBean is a request scoped bean with an injected 'SessionBean'. It has an postconstruct method wherein the value from the sessionBean is retrieved and set to a feild. + +[source,java] +---- +public class AnswerBean { + + @Inject + private SessionBean bean; + + private String value; + + @PostConstruct + public void init() { + value = '{' + bean.getName() + '}'; + } + + public String value() { + return value; + } +} +---- + +== OutputServlet + +OutputServlet is another servlet with 'AnswerBean' as an injected feild. When '/name' is called the value from 'Answerbean' is read and written to the response. + +[source,java] +---- +@WebServlet(name = "output-servlet", urlPatterns = {"/name"}) +public class OutputServlet extends HttpServlet { + + @Inject + private AnswerBean bean; + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + final String name = bean.value(); + if (name == null || name.isEmpty()) { + resp.getWriter().write("please go to servlet /set-name please"); + } else { + resp.getWriter().write("name = " + name); + } + } +} + +---- + http://git-wip-us.apache.org/repos/asf/tomee/blob/21dfa9a7/examples/mp-metrics-metered/README.adoc ---------------------------------------------------------------------- diff --git a/examples/mp-metrics-metered/README.adoc b/examples/mp-metrics-metered/README.adoc new file mode 100644 index 0000000..f527082 --- /dev/null +++ b/examples/mp-metrics-metered/README.adoc @@ -0,0 +1,133 @@ +index-group=Unrevised +type=page +status=published + += Microprofile Metrics + +This is an example on how to use microprofile metrics in TomEE. + +== Run the application: + + mvn clean install tomee:run + +Within the application, there is an enpoint that will give you a weather weather status for the day and week. + +== For the day status call: + + GET http://localhost:8080/mp-metrics-metered/weather/day/status + +== Response: + + Hi, today is a sunny day! + +== Metered Feature +MicroProfile metrics has a feature that can be used to find the rate of requests to a service. + +To use this feature you need to annotate the JAX-RS resource method with @Metered. +[source,java] +---- + + @Path("/weather") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApplicationScoped + public class WeatherService { + + @Path("/day/status") + @Metered(name = "dailyStatus", unit = MetricUnits.MINUTES, description = "Metrics to daily weather status method", absolute = true) + @GET + @Produces(MediaType.TEXT_PLAIN) + public String dayStatus() { + return "Hi, today is a sunny day!"; + } + ... + } +---- +There are some configurations, as part of @Metered, that you need to know: + +**String name** +Optional. Sets the name of the metric. If not explicitly given the name of the annotated object is used. + +**boolean absolute** +If true, uses the given name as the absolute name of the metric. If false, prepends the package name and class name before the given name. Default value is false. + +**String displayName** +Optional. A human-readable display name for metadata. + +**String description** +Optional. A description of the metric. + +**String[] tags** +Optional. Array of Strings in the <key>=<value> format to supply special tags to a metric. + +**boolean reusable** +Denotes if a metric with a certain name can be registered in more than one place. Does not apply to gauges. + +**String unit** +Unit of the metric. Default for @Metered is nanoseconds. + +== Metric data + +Check the Metered metric doing a _GET_ request: + +== Prometheus format: + + GET http://localhost:8080/mp-metrics-metered/metrics/application/dailyStatus + +== Response: + + # TYPE application:daily_status_seconds_count meter + application:daily_status_seconds_count 1.2E-7 + # TYPE application:daily_status_rate_per_second meter + application:daily_status_rate_per_second 0.0 + # TYPE application:daily_status_one_min_rate_per_second meter + application:daily_status_one_min_rate_per_second 1.3376002644204984E-19 + # TYPE application:daily_status_five_min_rate_per_second meter + application:daily_status_five_min_rate_per_second 3.5942838529305413E-20 + # TYPE application:daily_status_fifteen_min_rate_per_second meter + application:daily_status_fifteen_min_rate_per_second 3.4665766454142955E-21 + + +== JSON Format: + +For json format add the header _Accept=application/json_ to the request. + + { + "dailyStatus": { + "count": 2, + "fifteenMinRate": 5.77762774235716e-14, + "fiveMinRate": 5.990473088217569e-13, + "meanRate": 0, + "oneMinRate": 2.229333774034164e-12, + "unit": "minutes" + } + } + +== Metric metadata +A metric will have a metadata so you can know more information about it, like displayName, description, tags etc. + +Check the metric metadata doing a _OPTIONS_ request: + +== Request + + OPTIONS http://localhost:8080/mp-metrics-metered/metrics/application/dailyStatus + +== Response: + + { + "dailyStatus": { + "description": "Metrics to daily weather status method", + "displayName": "", + "name": "dailyStatus", + "reusable": false, + "tags": "", + "type": "meter", + "typeRaw": "METERED", + "unit": "minutes" + } + } + + +== Test the application: + + mvn test
