Spring boot serving only one request at a time - spring-boot

I am using spring boot 2.7.6 with tomcat version 9.0.69
My code
#GetMapping("/start")
public String demo() {
Response output = null;
System.out.println("Inside API");
InputRequest request = new InputRequest();
request.setType("type1");
request.setSize(50);
request.setPageType("page1");
for(int i = 0 ; i<10000000; i++) {
System.out.println("ankit "+i);
}
System.out.println("Inside done");
return "Success";
}
When I am hitting the controller multiple times using URL: http://localhost:8000/start, then at a time only one requesting is served.
Other requests are served when first one is completed. I got to know this from log I put.
I hit the start endpoint 10 times from 10 different browser window and output is :
Inside API
Inside Done
Inside API
Inside Done
Inside API
Inside Done
Inside API
Inside Done
..
Ideally it should be
Inside API
Inside API
Inside API
Inside API
...
What am I missing. NO config in properties file. Using java 1.8

Related

Flux not returning any response SPRING BOOT / MONGO

I am doing the typical chat application with mongodb and spring boot, I declared a tailable cursor and all that stuff, I followed this tutorial https://jskim1991.medium.com/spring-boot-making-a-chat-application-with-webflux-and-mongodb-part-1-5ad09c88f2ce
but the problem is that my controller is not showing any response
Status is 200 but nothing there even if I add another message, I can't get it meanwhile I can see it on the console
this is my code
#GetMapping(value = "/chat/id/{chatId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Chat> getMessages(#PathVariable Integer chatId) {
return chatRepository.findByChatId(chatId).doOnNext(System.out::println)
.subscribeOn(Schedulers.boundedElastic());
}
Does anyone have a clue on what is going on ?

Fabric8 customResourceDefinitions test

I am working on Fabric8 unit test, now I am trying to create a CRD against KubernetesServer.
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
public class TestCertManagerService {
#Rule
public KubernetesServer server = new KubernetesServer();
#Test
#DisplayName("Should list all CronTab custom resources")
public void testCronTabCrd() throws IOException {
// Given
//server.expect().get().withPath("/apis/stable.example.com/v1/namespaces/default/crontabs").andReturn(HttpURLConnection.HTTP_OK, ?????).once();
KubernetesClient client = server.getClient();
CustomResourceDefinition cronTabCrd = client.apiextensions().v1().customResourceDefinitions()
.load(new BufferedInputStream(new FileInputStream("src/test/resources/crontab-crd.yml")))
.get();
client.apiextensions().v1().customResourceDefinitions().create(cronTabCrd);
}
}
When I ran it, I got the following error
TestCertManagerService > testCronTabCrd FAILED
io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://localhost:60690/apis/apiextensions.k8s.io/v1/customresourcedefinitions.
at app//io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:694)
at app//io.fabric8.kubernetes.client.dsl.base.OperationSupport.requestFailure(OperationSupport.java:673)
at app//io.fabric8.kubernetes.client.dsl.base.OperationSupport.assertResponseCode(OperationSupport.java:626)
at app//io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:566)
at app//io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:527)
at app//io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:510)
at app//io.fabric8.kubernetes.client.dsl.base.BaseOperation.listRequestHelper(BaseOperation.java:136)
at app//io.fabric8.kubernetes.client.dsl.base.BaseOperation.list(BaseOperation.java:505)
at app//io.fabric8.kubernetes.client.dsl.base.BaseOperation.list(BaseOperation.java:494)
at app//io.fabric8.kubernetes.client.dsl.base.BaseOperation.list(BaseOperation.java:87)
at app//com.ibm.si.qradar.cp4s.service.certmanager.TestCertManagerService.testCronTabCrd(TestCertManagerService.java:94)
I have a few of questions:
(1) In this case, I am using v1() interface, sometimes I saw example code is using v1beta1(), what decides this version? By the way, I am using Kubernetes-client library 5.9.0
(2) In my code , I comments out this line
server.expect().get().withPath("/apis/stable.example.com/v1/namespaces/default/crontabs").andReturn(HttpURLConnection.HTTP_OK, ?????).once();
What is this statement for? In my case, I want to load a CRD, then create a CR, what is "?????" in the statement?
Any ideas for stack trace? How to fix it?
I appreciate it in advance.
From the code which you shared, it looks like you're using Fabric8 Kubernetes Mock Server in expectations mode. Expectations mode requires the user to set the REST API expectations. So the code shown below is setting some expectations from Mock Server viewpoint.
// Given
server.expect().get()
.withPath("/apis/stable.example.com/v1/namespaces/default/crontabs")
.andReturn(HttpURLConnection.HTTP_OK, getCronTabList())
.once();
These are the expectations set:
Mock Server would be requested a GET request at this URL: /apis/stable.example.com/v1/namespaces/default/crontabs . From URL we can expect a resource under stable.example.com apigroup with v1 version, default namespace and crontabs as plural.
When this URL is being hit, you're also defining response code and response body in andReturn() method. First argument is the response code (200 in this case) and second argument is the response body (a List object of CronTab which would be serialized and sent as response by mock server).
This request is only hit .once(), if KubernetesClient created by Mock Server requests this endpoint more than once; the test would fail. If you want to hit the endpoint more than once, you can use .times(..) method instead.
But in your test I see you're loading a CustomResourceDefinition from YAML and creating it which doesn't seem to match the expectations you set earlier. If you're writing a test about creating a CustomResourceDefinition, it should look like this:
#Test
#DisplayName("Should Create CronTab CRD")
void testCronTabCrd() throws IOException {
// Given
KubernetesClient client = server.getClient();
CustomResourceDefinition cronTabCrd = client.apiextensions().v1()
.customResourceDefinitions()
.load(new BufferedInputStream(new FileInputStream("src/test/resources/crontab-crd.yml")))
.get();
server.expect().post()
.withPath("/apis/apiextensions.k8s.io/v1/customresourcedefinitions")
.andReturn(HttpURLConnection.HTTP_OK, cronTabCrd)
.once();
// When
CustomResourceDefinition createdCronTabCrd = client.apiextensions().v1()
.customResourceDefinitions()
.create(cronTabCrd);
// Then
assertNotNull(createdCronTabCrd);
}
Bdw, if you don't like setting REST expectations. Fabric8 Kubernetes Mock Server also has a CRUD mode which mock real Kubernetes APIServer. You can enable it like this:
#Rule
public KubernetesServer server = new KubernetesServer(true, true);
then use it in test like this:
#Test
#DisplayName("Should Create CronTab CRD")
void testCronTabCrd() throws IOException {
// Given
KubernetesClient client = server.getClient();
CustomResourceDefinition cronTabCrd = client.apiextensions().v1()
.customResourceDefinitions()
.load(new BufferedInputStream(new FileInputStream("src/test/resources/crontab-crd.yml")))
.get();
// When
CustomResourceDefinition createdCronTabCrd = client.apiextensions().v1()
.customResourceDefinitions()
.create(cronTabCrd);
// Then
assertNotNull(createdCronTabCrd);
}
I added CustomResourceLoadAndCreateTest and CustomResourceLoadAndCreateCrudTest tests in my demo repository: https://github.com/r0haaaan/kubernetes-mockserver-demo

Moving files to archive directory using apache camel ftp component

I have few files stored in a database, these files were fetched from a FTP server and processed.
I am trying to move these files to archive directory on FTP server and to do that I am using producerTemplate.
This is what I have done so far.
try {
DefaultCamelContext camelContext = new DefaultCamelContext();
ProducerTemplate template = camelContext.createProducerTemplate();
template.request("ftp://xxxxx/include=fileFromDb.xml&move=archive/fileFromDb.xml", outExchange -> {
String body = outExchange.getIn().getBody(String.class);
});
} catch (Exception ex) {
// update the status in database to indicate archive failed
}
But this is failing with following error and can't get this resolved.
WARN o.a.c.c.f.remote.RemoteFileProducer - Writing file failed with: Cannot write null body to file: archive/ID-192-168-1-113-tpgi-com-au-1627667653835-1-2
I have tried implementing solutions from other answers but they didn't worked for either.
If I write a custom route like below, it works fine but because I need perform post move actions I prefere using producer template.
from("ftp://xxxxx/include=fileFromDb.xml&move=archive/fileFromDb.xml")
.log("file moved successfully").
If I write a custom route like below, it works fine but because I need perform post move actions I prefere using producer template.
You could use onCompletion to call another route to handle the post move actions.
String sftpURI = "sftp:localhost:2222/upload" +
"?username="+username+"&password="+password
+ "&move=done";
String sftpPostMoveURI = "sftp:localhost:2222/upload/done" +
"?username="+username+"&password="+password
+ "&fileName=${headers.CamelFileName}";
from(sftpURI)
.routeId("ReadFileUsingSFTPRoute")
.onCompletion().onCompleteOnly()
.setBody().constant("")
.to("direct:postMoveActions")
.end()
.log("file ${headers.CamelFileName} moved");
from("direct:postMoveActions")
.routeId("PostMoveActionsRoute")
.log("doing post move actions!")
.pollEnrich().simple(sftpPostMoveURI).timeout(5000)
.log("File: ${body}");

Spring xd stream web service responses

I am trying to use spring xd to ingest text/xml responses from a web service using HTTP/1.1 protocol. The goal is to convert the xml response to json and insert into mongodb. But right now I am unable to get any responses from the stream. I want to do this programatically rather than in the shell, below is my code
public static void main(String[] args) {
SpringXDTemplate template = null;
try {
template = new SpringXDTemplate(new URI("http://localhost:9393"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
String name = "test";
String definition = "time --fixedDelay=5 | http-client --url='''http://www.ctabustracker.com/bustime/api/v2/getvehicles?key=key&vid=1''' | file";
template.streamOperations().destroy(name);
template.streamOperations().createStream(name, definition, true);
}
I'm expecting to find the responses written to C:\tmp\xd\output\test.out but no file was created. I see that the stream was created in the admin ui, I'm not seeing any exceptions. How do I obtain the responses from sending requests to this url?
Turn on DEBUG logging; you should see the time source emit a message every 5 seconds; follow the messages through the flow to figure out what's going wrong.

Play framework multiple ajax request

I have an rest api with Play 2.3.x on a Docker container. This api speak with PostgresSQL 9.4 database (on a Docker container too) and serves JSON. Client is a isomorphic app with React (on Docker container too).
When client change route, several request is send to the api. The problem is that every request is abnormally slow. For example, I have this uri:
GET /api/taxon/185214/geojson
When I send this request directly since the browser, response is shown at the end of 4s. When this request is send with other request, it takes 12s longer.
What can cause this time ?
Of course, every action of api are async. For example, geojson action:
public static Promise<Result> showGeoJson(final Long id)
{
Promise<GeoJsonModel> promise = F.Promise.promise(
new Function0<GeoJsonModel>() {
public GeoJsonModel apply() {
// Get data with ebean
return geojson;
}
}
);
return promise.map(
new Function<GeoJsonModel, Result>() {
public Result apply(GeoJsonModel geojson) {
// return Result
}
}
);
}
Does each request should be treated in another thread ?
EDIT
So, after several tests, it's just my development computer whish is slow. Indeed, on other computer the response time is normal.

Resources