Avoid multiple request java web applicaiton - java

Third party integration, browser redirection function is triggering multiple request (only IE), 2 requests within 2 nano sec difference. I am using request attribute to deduct duplicate request and processing correct request(first request), duplicate request is sending error message. Unexpectedly, the browser is getting an error on the first request and stops further process(redirect to another component due to error).
Because error response is sending immediately, correct request is taking few nano seconds for processing the business logic.
How can I stop the duplicate request? Can I put Thread.sleep(20 sec) in error request , so duplicate request will be redirected after 20 sec by that time correct request will be processed and redirected to browser.
Is this advisable?

If I understand your question correctly, your problem is : have 2 requests and they send them together to your web application, but 1 of requests got error message.
If it's correct. The solution will be depend on the expected of the client, what's kink of response they expect on this case. We have 2 ways to go :
Return the error message when 1 message have problem to the client
Ignore error request, and send the response for the second request.
In my point of view , don't use this one :
Thread.sleep(20 sec)
What's happen if your request take more than 20 sec to process.
Hope it helps.

Related

Angular 5 Promise waiting to long for back end answer gets undefined

I'm sending a POST request from front-end within a Promise using Angular 5 and the problem is that back-end service takes to long to answer and, even though the script keeps going on Java back-end, promise's results shows up as "undefined", kind of like it has a timeout on waiting.
It is expected this backend service to take a while to answer. So, I intend to show to user that request stills not completed. To do so, I would need the Promised to keep on waiting a few more moments on the service's answer.
Chrome's console show this
Loader Started - HTTP request: /api/example/exampleService
POST https://localhost:4200/api/example/exampleService net::ERR_SPDY_PROTOCOL_ERROR
Http failure response for (unknown url): 0 Unknown Error
Loader Finished - HTTP request: /api/example/exampleService
You could increase your timeout using a pipe in Angular:
this.http.post(API_URL, {headers: HEADER})
.pipe(
timeout(3000) //3 seconds
);

Client gets 400 bad request but server did't receive any request

It's pretty wired but true. We are a back-end web-service team and recently our client started complaining they are getting sporadic 400 error for a GET request (this is the 2nd time in 2 week) but from server side we don't have access log either with the request-id or error code.
It violates the basic rule of client-server architecture, I have no clue what's going wrong. Any help is highly appreciable.
From client log:
2017-01-30 16:44:43,507 [nio-8009-exec-22] WARN java_class - call https://getcall.com/api [request_id] with params {app_id, start_time_in_mills , sender_id, type, pagesize, end_time_in_mills} returned error code 400-Bad Request with error
It was a problem with HTTP header, we were sending large amount of data(encrypted acls) in header which was exceeding the default limit from LB side(type-7), hence the request was not routing to the node and rejected from LB. Two solution here:
Increase the default header size for your LB/Server
Consider breaking headers and move it to body wherever possible.

Prevent multiple HTTP POST request in angularjs

I've built a complex web app using angularjs, java and hibernate.
I'm using http request for saving the form data.
$http({
method : 'POST',
url : $scope.servlet_url,
headers : {'Content-Type' : 'application/json'},
data : $scope.deals
}).success(function(data) {
}).error(function(data) {
});
I'm using angular version 1.2.19.
When hitting the save button this request will be triggered with the data available in the form and it moves to the server where the data is saved in the database. Before saving into the database many validations are done and also some external data is fetched which are related to the form data. Hence, it takes some time to save. (approximately around 5 to 7 minutes based on the form data provided). After the data is saved i'm redirecting to another page based on the response provided.
But the response takes time in my case, and in the mean time the same request is triggered again without any clue. There's no place the request is called in the code, but the request is triggered. It's a bit confusing.
But the same code works fine if the save takes less than 5 minutes. If it exceeds 5 minutes, it goes into a infinite loop and the save happens as many times the request is triggered. The response for the first request hits the angular controller but the controller doesn't identify the response, means we can't handle the response in this case. If this happens the page gets struck and we manually need to refresh or move the page.
Is there any way to prevent the duplicate request in angularjs? If there is a way, could anyone please help me achieve it.
Thanks in advance.

REST with JAX-RS - Asynchronous call

I saw the below post from "cmd" which was posted couple of years back. And "Wojtek Owczarczyk" was answered this one. I am good with all the answer, except last line.
My Confusion is, If we return immediately with ACCEPTED status. Then, we will lost the track of the request.
So i am planning to implement below steps. Please correct me if i am wrong.
1) As soon as the request hits service api - I will create one Job Id and persist my request detail and send back the client with ACCEPTED status code along with Job id.
2) Then, i will create the new thread for that request to continue with the requested operation.
3) After successful completion of Operation, I will send back the client with all status of the request.
4) Finally, in callbackCompletion register i will remove the job id from my persistence list.
To implement the above logic, i need client to send his listener information along with request (basically URI). This is to update the request status to client back, after processing the request.
REST with JAX-RS - Handling long running operations
This is not how REST is meant to work in my opinion. I would do the following approach instead:
Client makes a request for a long operation
Create a job id and run the job asynchronously
Return the accepted status together with the a URI to request the status for the job. For example: http://.../resources/jobs/1234
The client is now responsible e.g. to poll the URI to get the current status of the job execution.

Java servlet, respond after recieving each request before queuing

I am using Java servlets using Apache tomcat.
I've configured a threadpool and am dealing with each request.
My page is taking in many GET requests at the same time, I'm wondering if I can respond to the server after each get request before any of the logic happens?
So server gives me a request -> I respond with either 'good send another' or 'bad send another' before I start my queueing.
Any help would be much appreciated!
EDIT
Sorry that was terribly written :(
What I'm asking for is a way to send a Header to the client (in this case it's a server which sends me lots of requests). The response would just be 200 or error based on the information I get sent.
What my program is doing:
My servlet gets sent lots of GET requests from one client. (over 100,000) Which I am using tomcat to queue and put into a threadpool. It is then assigned to a worker thread which processes it and puts it into a database.
I've been told to do is send a request back to that server saying 'ok received it'. I think I can use a header response but I don't have the URL of that client (and the client can change for different campaigns). So was wondering what the best way would be to send that response.
After doing some more research I think what I'm looking for is ServletOutputStream.
response.setContentType("text/html");
ServletOutputStream output = response.getOutputStream();
output.flush();
output.close();
Using servlet output stream where do I set the <head><body> tag? and insert the header response afterwards.
The simple answer is "sure".
If these are get requests from a web page for a web page, include a refresh timer and send back some token that can be used to identify the difference between a first-time-request and an I-requested-earlier-are-you-done request. In this case the refresh timer can be set via a meta refresh tag.
If the get requests are part of a REST API then you can define "got it and I'm working" into the protocol. For instance, return a 202 to indicate "got it but not done" and return 200 to indicate "done". As with the html page, consider sending some token back with the 202 that identifies the pending request.

Categories

Resources