How to configure timer service in websphere liberty profile - ejb-3.1

I want to configure a timerservice in Websphere Liberty Profile.
I defined an EJB as follows, however it does not fire on the Application start.
I am using
<feature>ejbLite-3.1</feature>
Note: I cannot use ejb3.2 for the current project.
#Singleton
public class TimerEvery5Sec {
private static int i = 0;
#Schedule(second="*/1", minute="*",hour="*", persistent=false)
public void doWork(){
System.out.println("timer: " + i++);
}
#PostConstruct
public void afterCons(){
System.out.println("TimerEvery5Sec intiated");
}
public String callMe(){
return "TimerEvery5Sec - called";
}
}
Does liberty profile for ejbLite3-1 support timers?
if so, what should be done to register it in the server.xml

The Liberty profile feature ejbLite-3.1 does not support timers. Non-persistent timers were not added to the 'Lite' feature set in the EJB specification until version 3.2.

Related

Quarkus extension - How to have an interceptor working on beans provided by the extension itself

I'm working on a Quarkus extension that provides an interceptor (and its annotation) to add some retry logic around business methods this extension offers. Nothing new in there, and this is working when i annotate a public method of a bean in an application that uses this extension.
But the extension also provides some #ApplicationScoped beans that are also annotated, but the interceptor is not intercepting any of these.
Seems like an interceptor does not check / apply on the extension itself.
I would like to know if this is an intended behavior, or an issue in my extension setup, and if so how to fix it. Could not find anything about this in the documentation, but there is so much dos that i may have missed something.
Any idea about this ?
I finally found a way to make this work.
I was using a producer bean pattern to produce my beam as an #ApplicationScoped bean inside the extension.
#ApplicationScoped
public class ProxyProducer {
#Produces
#ApplicationScoped
public BeanA setUpBean(ExtensionConfig config)
{
return new BeamsClientProxy(new InternalBean(config.prop1, config.prop2));
}
}
with the following BeanA class (just an example)
public class BeanA {
private final InternalBean innerBean;
public BeanA(final InternalBean innerBean) {
this.innerBean = innerBean;
}
#MyInterceptedAnnotation
public void doSomething() {
}
}
Due to this setup, the bean is not considered by the interceptor (i guess because it's produced only the first time it's used / injected somewhere else)
Removing the producer pattern and annotating directly the BeanA fixed the issue.
Example:
#ApplicationScoped
public class BeanA {
private final InternalBean innerBean;
public BeanA(final ExtensionConfig config) {
this.innerBean = new InternalBean(config.prop1, config.prop2);
}
#MyInterceptedAnnotation
public void doSomething() {
}
}
with of course adding the following lines to register the bean directly on the extension processor:
#BuildStep
AdditionalBeanBuildItem proxyProducer() {
return AdditionalBeanBuildItem.unremovableOf(BeanA.class);
}
As a conclusion:
Changing the bean implementation to avoid the producer-based bean use case solved my issue (please refers to Ladicek comment below)
Edit:
As Ladicek explained, Quarkus doesn't support interception on producer-based beans.

Is it possible to fetch data from database before server is getting started?

I am working on a Spring project. I want to use scheduler in it and want to schedule it on a variable date. This date has to be taken from database. Is it possible to fetch data from database before server is getting started?
Two solutions come to my mind:
#PostConstruct annotated method of some #Component:
#Component
public class MyBean
{
#PostConstruct
public void init()
{
// Blocking DB call could go here
}
}
Application Events. For the ApplicationReadyEvent:
#Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent>
{
#Override
public void onApplicationEvent(ApplicationReadyEvent event)
{
// DB call could go here
//
// Notice that if this is a web services application, it
// would potentially be serving requests while this method
// is being executed
}
}

Google Appengine and rx-Java?

Is rxJava library compatible with Google Appengine? If so are there any limitations? The only info I have found is mention of 'partial support' on grepcode. http://grepcode.com/snapshot/repo1.maven.org/maven2/com.netflix.rxjava/rxjava-core/0.9.0
What is not supported?
You should create a child of RxJavaSchedulersHook and override its methods using your scheduler which use com.google.appengine.api.ThreadManager:
I've done it like this :
public class MyThreadSchedulersHook extends RxJavaSchedulersHook {
private final Executor executor = new ScheduledThreadPoolExecutor(10, ThreadManager.backgroundThreadFactory());
#Override
public Scheduler getComputationScheduler() {
return Schedulers.from(executor);
}
#Override
public Scheduler getIOScheduler() {
return Schedulers.from(executor);
}
#Override
public Scheduler getNewThreadScheduler() {
return Schedulers.from(executor);
}
}
Then you should register this hook. Better to do this in an ServletContextListener implementation:
public class ContextListener implements ServletContextListener {
#Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
RxJavaPlugins.getInstance().registerSchedulersHook(new RxMainThreadSchedulersHook());
}
#Override
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
// App Engine does not currently invoke this method.
}
}
It works for me.
The problem is the limitation of Java Threads in Google Appengine: https://developers.google.com/appengine/docs/java/#Java_The_sandbox
RxJava uses Java Thread and Executor in the Scheduler implementations. So the codes which get involved some concurrent Schedulers can not run in Google Java Sandbox.
If you want to use Scheduler in Google Appengine, you need to implement a special Scheduler by yourself. Besides, some operators may use Scheduler by default, such as delay use Schedulers.computation() by default. Remember to use their Scheduler overload methods.

Registering Startup Class In Nancy Using AutoFac Bootstrapper

I've been reading through a lot of the Jabbr code to learn Nancy and trying to implement many of the same patterns in my own application. One of the things I can't seem to get working is the concept of an on application start class. The Jabbr code base has an App_Start folder with a Startup.cs file (here) in it with the following implementation.
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
...
SetupNancy(kernel, app);
...
}
}
private static void SetupNancy(IKernel kernel, IAppBuilder app)
{
var bootstrapper = new JabbRNinjectNancyBootstrapper(kernel);
app.UseNancy(bootstrapper);
}
When I tried to do something similar to that in my project the Startup.cs file was just ignored. I searched the Jabbr code base to see if it was used anywhere but I wasn't able to find anything and the only differences I could see is Jabbr uses Ninject while I wanted to use AutoFac
Is there a way to register a startup class in nancy?
Take a look at my project over on GitHub, you'll be interested in the Spike branch and may have to unload the ChainLink.Web project to run I can't remember.
I had some trouble finding a way to configure the ILifetimeScope even after reading the accepted answer here by TheCodeJunkie. Here's how you do the actual configuration:
In the bootstrapper class derived from the AutofacNancyBootstrapper, to actually configure the request container, you update the ILifetimeScope's component registry.
protected override void ConfigureRequestContainer(
ILifetimeScope container, NancyContext context)
{
var builder = new ContainerBuilder();
builder.RegisterType<MyDependency>();
builder.Update(container.ComponentRegistry);
}
The application container can be updated similarly in the ConfigureApplicationContainer override.
You should install the Nancy.Bootstrappers.Autofac nuget, inherit from the AutofacNancyBootstrapper type and override the appropriate method (depending on your lifetime scope requirements: application or request). For more info check the readme file https://github.com/nancyfx/nancy.bootstrappers.autofac
HTH
After following the advice from TheCodeJunkie you can use the Update method on the ILifetimeScope container parameter which gives you a ContainerBuilder through an Action:
protected override void ConfigureRequestContainer(ILifetimeScope container, NancyContext context)
{
container.Update(builder =>
{
builder.RegisterType<MyType>();
});
}

Class xxx does not contain a public constructor needed to autobuild when xxx become a Tapestry Service

I'm discovering the wonderful integration work made by Tynamo's team between Tapestry and Resteasy .
I'm trying to activate Liveclass Reloading on webservices. As per doc says :
Documentation
The only thing you need to do to enable live class reloading for your
REST services is to bind them as regular Tapestry IoC services and
contribute them to javax.ws.rs.core.Application.class. Read more about
how service implementation reloading works in:
http://tapestry.apache.org/reload.html
Here is an example from the tapestry-resteasy test suite.
public static void bind(ServiceBinder binder)
{
binder.bind(ReloadableEchoResource.class, ReloadableEchoResourceImpl.class);
}
#Contribute(javax.ws.rs.core.Application.class)
public static void configureRestResources(Configuration<Object> singletons, ReloadableEchoResource reloadableEchoResource)
{
singletons.add(reloadableEchoResource);
}
My Own Work
This is exactly what I'm doing (well ... hmmm at least I believe that it is ;D):
My binding
public static void bind(ServiceBinder binder)
{
binder.bind(PushMessageService.class, GCMPushMessageServiceImpl.class);
binder.bind(UserService.class, HibernateUserServiceImpl.class);
binder.bind(IUserResource.class, UserResourceImpl.class);
}
/**
* Contributions to the RESTeasy main Application, insert all your RESTeasy singletons services here.
*/
#Contribute(javax.ws.rs.core.Application.class)
public static void configureRestResources(Configuration<Object> singletons, IUserResource userResource)
{
singletons.add(userResource);
}
My Interface
#Path("/user")
public interface IUserResource {
/**
* Lecture de tous les utilisateurs
*
* #return une List des utilisateurs existants
*/
#GET
#Produces("application/json")
public abstract List<User> getAllDomains();
Error
But when I start my app, I obtain this message :
HTTP ERROR 500
Problem accessing /user. Reason:
Exception constructing service 'ResteasyRequestFilter': Error building service proxy for service 'Application' (at org.tynamo.resteasy.Application(Collection) (at Application.java:14) via org.tynamo.resteasy.ResteasyModule.bind(ServiceBinder) (at ResteasyModule.java:31)): Error invoking service contribution method org.tynamo.resteasy.ResteasyModule.javaxWsRsCoreApplication(Configuration, ObjectLocator, ResteasyPackageManager, ClassNameLocator): Class com.sopragroup.ecommerce.mobile.rest.IUserResource does not contain a public constructor needed to autobuild.
Caused by:
java.lang.RuntimeException: Exception constructing service 'ResteasyRequestFilter': Error building service proxy for service 'Application' (at org.tynamo.resteasy.Application(Collection) (at Application.java:14) via org.tynamo.resteasy.ResteasyModule.bind(ServiceBinder) (at ResteasyModule.java:31)): Error invoking service contribution method org.tynamo.resteasy.ResteasyModule.javaxWsRsCoreApplication(Configuration, ObjectLocator, ResteasyPackageManager, ClassNameLocator): Class com.sopragroup.ecommerce.mobile.rest.IUserResource does not contain a public constructor needed to autobuild.
at org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator.obtainObjectFromCreator(JustInTimeObjectCreator.java:75)
at org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator.createObject(JustInTimeObjectCreator.java:54)
It's quite like the autobinding don't works (indeed I do think it is).
Obviously, when I try to without creating an interface and binding, it works like a charm.
Can someone give me a clue ?
I think the issue is that tapestry-resteasy is trying to autobuild IUserResource because it's in the "rest" package.
Here is a very important documentation line that you may have missed:
One more thing: DO NOT put this service in the autodiscovery package.
This is an important line and it was somehow hidden in the docs so I added a warning to make it more visible for future users: http://docs.codehaus.org/pages/diffpagesbyversion.action?pageId=151847035&selectedPageVersions=24&selectedPageVersions=23

Resources