Make #RefreshScope default - spring-cloud

If I annotate a bean as #RefreshScope, I can get a new instance of it after its configuration changes (eg. by triggering the refresh by calling /refresh).
But this is exactly I'd like for each of my beans: why would I change a configuration file and then expect the configuration to take effect for some part of the application immediately and for some part only after restart?
So the question is whether it's possible to apply it as a default scope?
Also in a typical Spring Boot application a lot gets auto-configured (eg. datasource), and without a default scope, I'd have to build the beans myself and annotate them properly. (edit: #ConfigurationProperties are automatically refreshed, and since a Spring Boot Datasource auto configuration is based on that, it is refreshed indeed without #RefreshScope)
What am I missing here?

https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_environment_changes and https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_refresh_scope has all the answers.
#ConfigurationProperties are automatically refreshed when /refresh is called, so beans using these properties get the fresh values, for the others and #Value there's #RefreshScope.
I don't think making #RefreshScope default is possible.

The beans annotated with #RefreshScope don't get automatically refreshed after a configuration is changed. It gets refreshed only after the cache entry is invalidated.
From the docs:
Refresh scope beans are lazy proxies that initialize when they are used (i.e., when a method is called), and the scope acts as a cache of initialized values. To force a bean to re-initialize on the next method call, you just need to invalidate its cache entry.
One way to invalidate the cache is by using the /refresh endpoint.
It's valid to note that a refresh-scoped bean can lead to unexpected behavior, please refer to the docs mentioned above to understand why this is not the default behavior.

Related

Is Database Context shared between sessions in Entity Framework?

In Startup.cs it's possible to control dependency injection lifecycle using transients and singletons. However it's unclear how the lifecycle works when using .AddDBContext like so services.AddDbContext<DatabaseContext>(...);
Each controller uses this dependency by initialising it only once in the constructor and is reused throughout by the controller functions.
Is the context initialised for each request or is there a possibility this context being shared between user sessions resulting in bad state?
Note: duplicate question does not address if context is being shared between user sessions.
services.AddDbContext<>(...); registers your DbContext with Scoped lifetime. That means a new instance is created for every single request. No need to worry it would be shared with other connections.

Hit REST end point on startup - weblogic + ATG

I have a rest endpoint which would start the scheduler of loading a XML to memory. Whenever I hit that rest endpoint, it loads the XML in memory and would return the XML after its ready (would take 10 - 15 seconds). When the same endpoint is accessed again, it would return the cached XML. Everything works fine but for now I have to manually hit the endpoint for the scheduler to start. Is there a way to hit the endpoint automatically via a simple code in startup? Or is there any other solution for this?
Normally, a component in the Nucleus is instantiated at first access, not at system start-up.
The way to have anything done at start-up in ATG is to create your component, and then to add its nucleus path to the list of initial services in the /Initial component (or from one of the many other Initial components changed off of it)
The component should be globally scoped. Because /Initial is instantiated at start-up, the services it references will also be instantiated as dependencies.
If your component is a POJO, then the no argument constructor will be invoked on component start-up, then the setX method will be called for each property with a value defined in its properties file.
If your component is extended from Generic Service, then additionally, beforeSet and afterSet methods will be called, before and after the set methods are invoked, if they exist, and finally doStartUp will be called.
This is all part of the fundamental lifecycle of components that the Nucleus manages.
This gives you a number of hooks with which to invoke your custom code.
Now, in your question, you ask how to call a REST endpoint at start-up. However, I believe what you actually want to ask is how to ensure that a particular piece of code gets executed at system start-up. A REST endpoint is how you are triggering it today, manually, from outside the Nucleus. But that does not mean that it must call a REST end point if it is to be automatically called at start up.
The easiest way to achieve what you want is
define a class that extends GenericService
override the doStartUp method
put the code you want to execute in this method, or invoke the code on another component from here
define a globally scoped component for the class
Add the component to the initialServices property of the Initial component
Restart the server and check that your code is being called at start-up. Put some debug statements in, and switch debug logging on in your layer.
Note, you may actually also want to think about whether you really need to invoke your code at system start-up. Anything in initial services adds to the start time of the server. Depending on your requirements, it may be better to do it on first access of your application service rather than at server start-up.

Using java Aspectj/Annotation to log http request in Spring Boot

I have created an Aspectj that is triggered when methods are annotated with #AuditLog.
The purpose is to log all the request parameters and body. The issue that I'm having is that inside the Aspectj the InputStream has already been consumed, probably by spring when creating the #RequestBody parameters in the rest method.
I don't want to use a filter with a wrapper since I want the least invasive modification to the actual code and also I only need to log certain methods.
Can this be done inside the Aspectj?
Edit. More Info added.
I wanted to do this in The AspectJ because I don't need to log all the methods, I also need to read information from the properties file and added to the log message. This log has to be saved to a queue for later processing.
A could use a wrapper inside the AspectJ but the InsputStream has already been consumed.

How to prevent or queue calls to the JBoss server before it finishes initializing everything?

I have an EAR I deploy on JBoss. This EAR contains two main components, a set of EJBs, and a servlet. It also contains several other libraries.
In the current state of the code, there is a need to initialize some singletons so that the EJB can actually work. This init is made in the servlet, to start all data caches and initialize them. My problem is that because of this separate initialization, which can last up to 30 seconds, there is a moment during which the EJB are deployed and available, but calling them would lead to errors.
So the question is: is there a way to lock the EJB calls for the time of this initialization by the other component? Ideally, it would just queue them and execute them once the init finished, but it could also refuse them.
The only way I can think of for now would be to have a static boolean in the initializer, which would be checked in the EJB calls (their interceptor, actually). But this is far from convenient. So I'm wondering if there is another way to do it, less dirty.

Jboss Service / Managed Bean Question

I have a managed bean / service running inside of JBOSS. I then have a quartz job that will occasionally wake up and call a method of the managed bean. This method is sometimes long and drawn out, and since I don't want the quartz job to time out, I have implemented a thread within the managed bean to perform the processing. When the thread is finished I need to update a database table with the results. This is a very serial process and it needs to be based upon some business rules, etc.
My main question is that I can use an EntityManager within the service without a problem however I can not use it from within the thread, I get a NullPointerException. What would be the best way to address this?
Thanks,
Scott
As creating threads in appservers is discouraged, I'd modify the setup a bit.
I'd move the core of processing to a message driven bean, and have the Quartz job just send a message to the queue on which the MDB is listening. The MDB in turn can call your EJB, and like this everything remains within what's allowed by the standard.
As per the documentation and specification the Entity Manager is not thread safe and can not be used across different child threads as I had originally had in mind. I ended up going back to the original design similar to the one provided by fvu, however I found some annotations that would allow me to modify the been timeout period and allow the long running process to work properly. Here's the annotation that I used:
#PoolClass(value=org.jboss.ejb3.StrictMaxPool.class, timeout=360000000L)