how to call a method in Stateless EJB during deployment? - ejb-3.0

How can i tell a application server to call a method in my stateless ejb so that I can print some message during deployment. If I annotate the method with #PostConstruct then it is not working. I also added static block still not working. I dont want to use MBean. Also my ear does not have any web project. So I can not use any servlet and its init method to print such messages.
Server we are using is Jboss.
thanks

There is no such mechanism in EJB 3.0.
Eager / auto loading of EJB / load EJB on startup (on JBoss)

Related

main() method in a spring mvc application

My understanding of a basic Java application is that every application should have a main() method but when I wrote a Spring MVC application (not Spring Boot), I don't think there was ever a main() method that I wrote. Isn't it mandatory? What am I missing here? Or is this implemented in Spring somewhere internally? If yes, where is the main defined?
Spring MVC is just a Servlet based framework which can only be run inside Servlet container like Tomcat or Webligic. The main method is deep in Servlet container that when you start will go and load war file into Java VM and delegate HTTP calls made to it to the appropriate Servlet that in turn will delegate to your Spring controller. For example when you run Tomcat startup.bat or startup.sh scripts they eventually will run Java main method.
SpringMVC applications are typically run within an application server, for example Tomcat, so there is no main method like a traditional java program. SpringMVC has a servlet that is loaded by the application server and starts the webapp.

#EJB not working in websphere liberty 8.5.5.9

We are trying to migrate from websphere application server to websphere liberty profile.
As part of POC we are deploying a sample war with an EJB inside it. We injected the EJB using #EJB inside a restful web service. While calling the web service liberty is throwing NPE. Then i tried using InitialContext to lookup the EJB and its working fine.
Below are the features i enabled
Jaxrs 1.1
Jdbc 4.0
ejbLite 3.1
We are running liberty on java 1.6.
Below is some code snippet
#EJB(beanName="StudentServiceBean")
private StudentServiceBean service;
Inside web service method
service.getStudent(id); // NPE here
Ejb class
#Stateless(name="StudentServiceBean")
public class StudentServiceBean {}
I even tried using #EJB with lookup and then tried with simple #EJB becUse the EJB is a local EJB and packed inside the war module itself
P.S.
I moved the ejb to an ejb module and packed the application as an ear and tried. Still the ejb is not getting injected into the web service.
Then i created a new ejb and injected the new ejb into the existing ejb. The new ejb is injected properly. I think there is some problem with servlet container and the ejb container.
I tried it on liberty woth jee7 features also but still i am facing the same problem.
If the #EJB annotation cannot be resolved to an EJB, then you would have received a failure when the server created an instance of the class containing the #EJB annotation. Since that does not appear to be happening for you, and instead the instance is created fine, just without that field being set, then the server is not scanning your class for annotations or you have the javax.ejb.EJB annotation class packaged as part of your application.
I would recommend checking the following:
Make sure the javax.ejb.EJB class is not being packaged as part of your annotation
Check that the web.xml for your WAR module has a version > 2.4. WAR modules with a version of 2.4 (or earlier) will not be scanned for annotations.
Check that the web.xml does not contain the setting metadata-complete="true". This setting turns off annotation scanning.
JAX-RS 1.1 is part of EE6. The EE6 platform spec does not list JAX-RS endpoints among the EE components supporting injection (Table EE.5-1). I don't think your usage there is portable.

Deployment lifecycle when init method is called

Hi I am newbie to Servlet/JSP/Springs .Wanted to know
I have a ear file which I deploy on weblogic server . Now as part of this ear some modules are exposed as webservice.
Now I have spring bean in the above mentioned ear on which init method I am trying to access the webservice method's which should be exposed as part of deployment of this particular ear.
Will I be able to access the webservice or yet till init method is called the modules are not exposed as webservice
So basically when I do the deployment first the init method of spring bean will be executed or
the modules which should get exposed as webservices will get exposed .
The init method will be executed first during which your web service will be initialized after which the web service will get exposed.

Run code on a Spring MVC app deployment

I need to run some code when my application is deployed into a web container — and that class needs to be injectible by the Spring container.
I tried implementing ServletContextListener and registering the class as a listener, but injection wouldn't work.
What's the idiomatic way of doing this?
To make your class injectible by Spring container, you can extend SpringBeanAutowiringSupport.

EJB3 initialization code

I've searched this for a while now, but can't seem to find an answer to this. How can I execute some code in when deploying an EJB3 jar-file to a JBoss server? For example, I need to run some sql migration scripts before the beans are ready to be used.
If you can't use EJB 3.1 (with #Singleton #Startup), I would recommend packaging your EJB module in an EAR with a WAR. Add a ServletContextListener to the WAR, and take your action in the contextInitialized method.
You can create a JBoss MBean service with a Listener that can perform any initialization (SQL scripts run in your case) after JBoss is fully started and before any EJB is used.
I have created such a service and we run it on JBoss 4.2.3.GA, so, you do not need to migrate to JBoss 7.

Resources