Please help. How to configure RestClientProxyFactoryBean in spring boot project
Thx
See this example: It creates a rest client service which you can call to get a rest client instance to make http calls
https://www.programcreek.com/java-api-examples/index.php?source_dir=expressui-framework-master/expressui-domain/src/main/java/com.expressui.domain/RestClientService.java
Usage of this can be seen in the Below link:
https://www.programcreek.com/java-api-examples/index.php?source_dir=expressui-framework-master/expressui-domain/src/main/java/com.expressui.domain/RestClientService.java#
And some test case to be run can be found here:
https://www.programcreek.com/java-api-examples/index.php?source_dir=expressui-framework-master/expressui-domain/src/main/java/com.expressui.domain/RestClientService.java#
Explanation:
So basically we can create different Restclients using proxyfactory bean. In the above example, a service named Geoplanetservice is being created. Rest communication happens via GeoPlanetClient service Interface which is the instantiated via our proxyfactory bean.
Related
I want to create a REST API in spring boot which can handle around 100TPS. Lets say I create a basic REST api using a sample application in spring boot. Does this automatically handles multi threading. Lets consider the code bellow.
#RequestMapping(method = RequestMethod.POST, value="findByPackageActivationId")
#ResponseBody
public JSONObject findByPackageActivationId(#RequestBody IncomingRestObject incomingRestObject) {
//My work here
}
By default Spring Boot web applications are multi-threaded and will handle multiple requests concurrently.REST controller is multithreaded as the DisptcherServlet handles multiple requests from the clients concurrently and serves using the respective controller methods.You can change any of the default thread settings as well (e.g. server.tomcat.max-threads). For more information refer here :-
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc
REST controller is multithreaded as the DisptcherServlet handles multiple requests from the clients concurrently and serves using the respective controller method
You can scale your app as per your requirements to achive desired TPS
https://dzone.com/articles/how-to-test-if-your-multi-threaded-java-rest-servi
For every request from the client, the server will create a new thread and assign that request to newly created thread. You don't have to handle this explicitly.
Also for your requirement(100 Threads), you can configure this on the server. You can refer this link for tomcat configuration as its default server for spri.
Spring Framework provide these feature internally, these are benefits that spring provide over core servlet coding that we used to do in older days. It also provide the way to control it.
For Ex you can change limit of number of thread to create using server.tomcat.max-threads (For tomcat).
For Basic understading on multi threading with spring boot please refer https://www.e4developer.com/2018/03/30/introduction-to-concurrency-in-spring-boot/
How to implement a clean up method after request finish process in a spring boot application for clean data such as thread locals
I try with ServletRequestListener.requestDestroyed api, but it does not get hit after the request finish
One possible answer is that you have neglected to register the listener. The Servlet javadocs say:
In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener, or registered via one of the addListener methods defined on ServletContext.
There may be other ways to implement this using Spring; e.g. using a handler intercepter; see Remove ThreadLocal object within a Spring MVC website?
If you were using plain servlets (without the Spring MVC infrastructure), another approach would be to do the cleanup in your servlet's service method or the doXxx methods. Or in a Filter in front of the servlet.
I am quite new to Spring and have a question related to Spring Service naming convention.
I have written a service, and used an annotation to define and name it.
#Service(value="CustomerService")
This service is implemented within a library that is used by a web app. Everything works fine and I can autowire my service into my client components.
Now I would also like to expose this service using http invoker. This works ok. I have define a /CustomerService http service which accesses CustomerService bean.
The issue I have is that one of my components, a client side component, that I used in my web app (CustomerDetailsValidator) can also be used in this new application.
In my CustomerDetailsValidator I have something like this:
#Autowired
#Qualifier(name="CustomerService").
But if I want to reuse my CustomerDetailsValidator and use it in my new app, this time I need to wire it to the httpservice instead.
Which means that the #Autowired and #Qualifier code is useless.
My question is what is the best practice in this case?
Should I still use #Service?
I guess I cannot use Qualifier anymore.
My feeling is that I should define everything in xml in each application context.
The web app using the library directly would just use the CustomerService bean as a singleton.
While my new client app would link the customer service id to the http service.
Is that a good approach? Do we have patterns for this?
Thanks and regards
Gilles
I have a Mule application, and I'm creating some unit tests including some Mock Web Services to test certain functionality. The Mock Web Services I'm creating using a test mule flow, so I'm registering an HTTP inbound endpoint.
When I run my test, I get a failure because one of my Spring objects fails to load due to the fact that it cannot reach my mock web service(The web service is loaded dynamically, so when doing the test, it resolves to localhost, otherwise it is usually an external service). I've made sure that when I do my getConfigResources() I load my mock web service flow file before I load my spring context beans, however I still get the error.
Is there a way to make Mule/Spring load my flow file and start up my http:inbound-endpoint before my Spring beans?
You could try setting the depends-on attribute on your Spring bean to the name of the Mule flow, but I doubt it will work and I'm not convinced it's something desirable anyway.
Otherwise, the only thing I can think of is that you initialize the Spring bean factory yourself in a #Before method of your functional test case. This will give you an opportunity to pass the value of the dynamic port you're using with your mock HTTP inbound endpoint (assuming you do use a dynamic port, which is the recommended approach).
Can or should a Service layer be a Spring bean?
If so, how should it be got from a calling application, a consumer of a service?
Because the consumer must be aware that such a bean exists, so it in any case must use Spring
to make use of Service methods.
Of course. The service layer is the part of your application that is visible to other users (e.g. a Web layer) so it needs to be configured and setup somewhere. Imho a Spring configuration is the best place to put this configuration in. The Service Layer user then has to take care of instantiating that context and getting the Service Objects he needs.
An alternative - if it needs to run standalone - would be for your service class(es) implementing the Service Layer interface(s) to instantiate the Spring application context themselves.
By making your consumers also spring beans, and inject the service bean with dependency injection.
Yes, It is always nice to configure service beans as spring beans. In the web layer you need to take care of instantiating the needed service objects. Another option is to make the web layer classes also as spring beans and inject the necessary service layer spring beans. From the testing point of view also, this type of design is very helpful when we use Spring testing framework.