record http request process time in play framework2 - java

I'm developing an application with play framework in java, in order to evaluate the performance of my application, I want to record the time on processing each http request. Based on the official document provided, I can only find one place in Global.java, where I can override the onRequest method to record the start time of a request.
Is there any plugin or hooks in this framework so I can add my code like this way?
long start = System.getsystem.currenttimemillis(); processRequest(request); long end = System.getsystem.currenttimemillis();

You can achieve this by using Filters. There is a working example which is exactly what you are looking for.
https://www.playframework.com/documentation/2.3.x/ScalaHttpFilters

Related

Build a report in Apache Camel to measure transaction times

I want to make some performance tests on my Camel app (I'm using Spring Boot for development) and build a report based on the time that each transaction took to finish, but I'm not quite sure what will be the best approach (first time doing something like this).
This app is layered in three parts: each part will publish a message to a JMS queue, so the next part can pick it up. So, I know that I can get the elapsed time for each Exchange like this:
List<MessageHistory> historyList = exchange.getProperty(Exchange.MESSAGE_HISTORY, List.class);
for (MessageHistory history : historyList) {
history.getElapsed();
}
But, as I will have three different parts inside the app I will get three different Exchanges. Each message coming inside the app will have an ID inside the payload so maybe I can group each transaction using that (unless there is a more Camel-oriented way to do it that I'm not aware of).
I can write something custom to make the report I suppose, but I'm wondering if maybe there are some frameworks that I can use.
Any recommendations are appreciated
Thanks!
I suppose you want to capture the total time it took for the message to traverse in your application. If so, you can use camel interceptors.
You can capture the starting time using the FromInterceptor, save the time in a property, and get the end time using the sendToEndPoint interceptor. Calculate the time it took for the message to travel

How to invoke Event-based Gateway in Camunda using remote API/Spring Boot?

I was hunting some knowledge on Camunda BPM during my free time. Then I came across this Event-based Gateway component which made my past two days difficult. Basically I was watching this tutorial in which the gentleman very nicely tells all the key components of Camunda BPM and how to execute them. Let me brief the tutorial based on keeping my query in mind.
So, I created a Camunda BPM workflow similar to the tutorial looking like below:
But at the Event-based Gateway component the gentleman executed the pending instance using REST Api with below details (more detail):
Content-type = application/json
method = POST
URI = http://localhost:8080/engine-rest/message
body = {"messageName" : "PaymentReceived","businessKey" : "4"}
Then I was going through Camunda docs where I found another component i.e. Receive Task. But as per documentation you can invoke an instance of Receive Task using below Java Code:
ProcessInstance pi = runtimeService.startProcessInstanceByKey("DefinationKey");
Execution execution = runtimeService.createExecutionQuery().processInstanceId(pi.getId()).activityId("ActivityId").singleResult();
runtimeService.signal(execution.getId());
But then out of curiosity I executed Rest API of Event-based Gateway on Receive Task and it worked without an issue. So, I was expecting vice versa should work, means execute an instance of Event-based Gateway using above code. But it did not.
So my question is, is invoking an instance of Event-based Gateway using Rest API is the only way or is there any alternate way which I missed from the document?
Answer:
This is a lame query, because I am trying to execute instances of Event-based Gateway instead of Payment Confirmed component as shown in image.
Below is the screen shot which enlighten the idea in more details:
So the code snippet required to move the instance from Payment Confirmed is as below
List<MessageCorrelationResult> results = runtimeService.createMessageCorrelation("PaymentReceived").correlateAllWithResult();
results.stream().forEach(result->{System.err.println(result.getExecution().getId());});

Events handling between Spring boot and Reactjs

This might seem like an easy solution got on the internet, but believe me, I have seen through a lot of examples and couldn't figure out which approach to choose.
Requirement :
I have a subscriber at the application service(spring boot/Java) end, subscribed to blockchain events( corda ). I want to push this event to UI (ReactJS) whenever there is a change in state.
I could subscribe to the blockchain events successfully but stuck with multiple in-complete or tangled ideas of pushing it to the UI and how UI would receive my events ( kindly don't suggest paid services, APIs, Libraries etc ).
I have come across and tried out all approach, since I'm newly working on events I need some ray of light as to how to approach towards a complete solution.
Publisher-subscriber pattern
Observable pattern
Sse emitter
Flux & Mono
Firebase ( a clear NO )
+Boggler :
events handling between service and UI , should it be via API/endpoint calls or can it be emitted just in air( i'm not clear) and based on event name can we subscribe to it in UI ?
should i have two APIs dedicated for this ? one trigger subscribe and other actually executes emitter ?
If the endpoint is always being heard doesn't it needs dedicated resource ?
I basically need a CLEAR approach to handle this.
Code can be provided based on demand
I see you mention you are able to capture events in Spring Boot. So you are left with sending the event information to the front-end. I could think of three ways to do this.
Websockets: Might be an over-kill, as I suppose you won't need bi-directional communication.
SEE: Perhaps a better choice than WebSockets.
Or simply Polling: Not a bad choice either, if you are not looking for realtime notifications.
Yes Long Polling.
The solution seems to be pretty simple. Make the connection once and let them wait for as long as possible. So that in the meanwhile if any new data comes to the server, the server can directly give the response back. This way we can definitely reduce the number of requests and response cycles involved.
You will find multiple implementation examples of How Long Polling is done as part of Spring Boot project on internet.

Extending or Overriding a REST API without disturbing client

I am working with Spring Batch Admin API to have Admin Screens working for my batch Jobs.
I am using this client
In above code of BatchJobInstancesController , UI is very slow for End Point - instancesForJob(...). Its slow because of too much unnecessary data not needed by UI being added.
So I am trying to write a new service or end point that replaces/overrides only that end point or service without least disturbing angular client.
How to approach it?
Service method SimpleJobService.getJobExecutionsForJobInstance needs to be overridden to change the logic.
How can I disable only that end point and plug in new code?
is it possible for new code to serve at same URL?
I mean this seems a common scenario where you are trying to use somebody else's N number of services but want to tweak only a few services.
EDIT: No answer from long time, I will try on lines mentioned here

Facebook Graph API Pagination - Facebook4j - persisting the since and until

I have been using Facebook4j for a Facebook graph API related requirement and my requirement is quite simple.
Requirement : I need search Facebook objects (all public objects- posts/comments/pages/ etc..) for given keywords frequently and persist all results into the db.
Problem : Although the requirement looks straight forward I am stuck in handling pagination of the results and calling the API later without losing any items ( posts/pages/comments ) in the consecutive calls to the API.
Doing some research I have found graph API provide several Pagination methods and Cursor based is the best and recommended. But unfortunately cursor based search is not available for all kind of objects. So I had to choose time-based pagination which uses until and since parameters.
Q1.Is my decision is correct?
Following is a sample previous and next URLs I get when I do a search API call using Facebook4j
previous = https://graph.facebook.com/v1.0/search?limit=200q=infographic&access_token=
[ACCESS_TOKEN]&since=1400152500&__previous=1,
next = https://graph.facebook.com/v1.0/search?limit=200&q=infographic&access_token=
[ACCESS_TOKEN]&until=1399983583
Say I did a API call and using the Facebook4j API method then I should be able to use
fetch next method and continue.
facebook.fetchNext()
But when ever I get an exception in my application OR at the point there are no further results I assume that I should be able to persist since/until values and use them for later API calls in the future and continue getting search results from where I stopped last time.
Q2. Is the assumption regarding the pagination correct ?
So I am assuming that my future API calls would be something similar to the below. I am not sure whether to use 'since' or 'until'.
Q3.Is the way I call the API below to continue searching from a previous search is fine ?
ResponseList<JSONObject> results = facebook.search("KEYWORD",new Reading().limit(200).until("1399983583"));
Also I am not sure whether these results are paginated in such a way I get the newest result set when I use the "NEXT url" / fetchnext() provided ?
Q4.Please clarify me on now the NEXT url provided exactly works in terms of pagination and future API calls?
If my above approaches are wrong, please suggest the best practices which I should follow to handle this requirement.

Categories