How to auto execute a session bean's method on ear deployment? - deployment

I need a way to have a session bean's method executed periodically every week at a certain time. I am thinking of using the quartz scheduler to do this. My question is, how do I start the scheduler to run from the moment the .ear file is deployed automatically? Is there any annotation that I can use on the stateless session bean calling the scheduler.start method such as #PostConstruct or something?

The problem with #PostConstruct for a stateless bean is that something has to trigger the bean to be constructed.
There's no standard way to do what you're trying to do until EJB 3.1 (automatic timers or startup singleton session beans). Until then, you could use a dummy WAR with a ServletContextListener to set up your EJB timer.

Related

How can I let Tomcat run a command after it finishes deploying web application's .war files

We know that during Tomcat startup, it will deploy the .war files of its web applications. My question is after the deployment I need to run a command to modify a file inside WEB-INF/ of the web application which is generated after deployment, and I need to let Tomcat do this automatically for me, is this possible to achieve ? Something like post_run command after deployment.
I found that CustomEventHookListener can probably do this How to run script on Tomcat startup?, but this involves in making a new Java class, and I'm not allowed to do so. I have to figure out way to modify the existing Tomcat configs like server.xml or tomcat.conf in TOMCAT_HOME/conf to do so.
The main issue about not using a Event Hook Listener is that there's no reliable way to tell if the application is ready or not, as Catalina implements it's own lifecycle for each of their components (as seen in https://tomcat.apache.org/tomcat-8.5-doc/api/org/apache/catalina/Lifecycle.html).
Your best shot is to use tail or some external program infer the component's state, but AFAIK there's no way to implement listeners directly in the configuration files.

Any way to detect if EntityFramework Core is run inside a tool?

Is there any way to detect if my application is being run through dotnet ef * or the PowerShell alternatives?
I'm creating a multi-tenant ASP.NET Core app with Entity Framework Core and I have logic to decide which connection string should be used, but should not be run if I'm creating migrations or updating a database through the command line. I cannot rely on checking for the existance of an HttpContext because I use my DbContext during configuration as well, nor can I use additional command line arguments or environment variables.
EDIT: Found a way to see if a connection string has already been configured, it's not ideal but I can use it to help with my particular use case:
optionsBuilder.Options.Extensions
.OfType<RelationalOptionsExtension>()
.FirstOrDefault()?.ConnectionString
If you create a type that implements IDbContextFactory<T> in your DbContext (or startup) project, the tools will call this instead of trying get your DbContext from services or instantiate it directly.

Eclipse 4 declarative service injection in a declarative service

I am creating declarative services, lets call them Service1 and Service2.
Everything works fine in my eclipse RCP application.
Now I want my Service1 to be injected into Service2, so my Service2 class have a new field like this :
#Inject
Service1 myService1;
This never gets injected and Debugging a little the Declarative Service instanciation in Eclipse 4 (org.eclipse.equinox.internal.ds.model.ServiceComponent) it seems that nothing get injected after the class has been instanciated.
Is this a limitation of the DS implementation in eclipse ?
Is there a way to fix this ?
Thanks.
No, it is not possible by default. You can make some utility class which is called on #Acivate which searching the coresponding service on tracker and sets the #Inject service with reflection, but in that case you will lose all of lifecycle, so its not a nice osgi way.
If you would line to use #Inject annotation you can use pax-cdi for that, but on that case you cannot use the declarative service annotations on that class - because in that case the pax will instantiate and register the service on the tracker.

folder observer in jboss

I need to write a folder observer that will run in JBoss. The idea is that as soon as a file will appear in a folder we should handle the file and move it somewhere.
I wrote a simple Java class that will use the Java 7 NIO and it observe the folder. Once a file exists appears in the folder I move it to another folder and process it. Currently what I have done is as soon as the file appears I move it and create a new thread to process it.
Since this suppose to run in JBoss I understand now that creating threads in JBoss is not the way to do that and I should use message queues.
Therefore I am about to change the class and call the queue and pass it an object of the file location and expect the JBoss to handle it.
My question is basically am I doing it right? is this the right way to do that in JBoss? Any process that I want to be done in a multi tasking way should be done with message queues?
Thanks in advance,
Sharon
You shouldn't create un-managed threads in a container. JBoss AS 7 / EAP 6 has the facility to let you define your own thread pools and permit the application container to manage these threads. If you want to use threads to do this, you should use a container managed thread pool. Otherwise, a message passing based implementation would work.
I am not fully sure why this application needs to run in a container/JBoss. Setting that aside, one option is to utilize the message oriented model using JMS queues/topics instead of dealing with files.
The other option is to create a JEE6 timer service that will execute your code to move files at a certain interval. So in this scenario JBoss will manage your threads using the ejb thread pool.

How to share a GWT RPC RemoteServiceServlet among multiple client modules / apps

I have several GWT modules and web apps running in Jetty. They each want to use my LoginService RPC interface, so I put this interface and its servlet implementation in a common module. I serve the servlet (LoginServiceImpl) from my root web context, and web.xml exposes it at the url "/loginService". In a another GWT module, to use this service, I had to set the entry point, like this...
LoginServiceAsync loginService = GWT.create(LoginService.class);
ServiceDefTarget t = (ServiceDefTarget)loginService;
t.setServiceEntryPoint("/loginService");
Now, the module trying to use the loginService is called discussions, and I get this error on the server.
ERROR: The serialization policy file
'/discussions/discussions/7B344C69AD493C1EC707EC98FE148AA0.gwt.rpc' was not found;
did you forget to include it in this deployment?
So the servlet is reporting an error that mentions the client (the discussions module). I'm guessing that the RPC plumbing passed the name of this .rpc file through from the client, and the servlet is now looking for it. (?) As an experiment, I copied, the *.gwt.rpc files from the discussions module into the root web context, so the servlet could find them. This did stop the error. But I still get another error:
com.google.gwt.user.client.rpc.SerializationException: Type
'mystuff.web.shared.User' was not assignable to
'com.google.gwt.user.client.rpc.IsSerializable' and did not have a custom field
serializer. For security purposes, this type will not be serialized.: ...
This class is serializable; it worked before in other modules, so now I'm lost.
What is the right way to use the LoginService from multiple clients modules?
Update:
This error was showing up in hosted devmode, and it went away after a full compile. Maybe this is related to gwt serialization policy hosted mode out of sync . I will update again if I can better reproduce the problem.
See my answer here. The short answer is: you'll need to make mystuff.web.shared.Users source available at compile-time to your discussions module.