How to init some functions in RESTful web service in java - java

I am utterly new to java web services and currently writing a RESTful web service in java. I want to call a method when the web service is deploying in order to initialize some values. How can I achieve this?..
I am using Axis2 as my deployment engine.
Thanks in advance

You can use a listener ServletContextListener.
When you will launch your application server, the "contextInitialized" method will be called and exactly do what you want like initialize a static configuration class ; depending on what you want to achieve.
EDIT :
You'll need to declare the listener into your web.xml like this :
<listener>
<listener-class>full.classpath.to.MyServletContextListenerImpl</listener-class>
</listener>
As an exemple, I use a listener in order to initialize my custom PoolManager which is a singleton.
I hope it can help you !

Related

Jersey 2.5: How can a resource access external classes instances

I have embedded Jetty container inside my main server and I also use Jersey 2.5 for handling REST resources.
everything seems to work well and now I would like to pass my server's context class into each of my REST resources.
I'm having hard time understanding how to do that...
If someone can provide full code example - it could be awesome!
Many thanks
What exactly do you mean when you say you have a Jetty container inside your "main server"? Are you programmatically executing Jetty within the application? Also, when you say "context" are you referring to the ServletContext?

Loading my ServletContextListener in Tomcat 7

New to Tomcat and running Tomcat 7 in an Eclipse environment.
I followed a tutorial for creating a ServletContextListener class. My question is, simply, how do I get this loaded and running in Tomcat? Can I use annotations? Do I need to edit an xml file?
Javadoc to the rescue:
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.
As an example in the web.xml:
<listener>
<listener-class>fully qualified class name of your listener class</listener-class>
</listener>
When you download Tomcat 7.0 from the Apache Tomcat website you'll get a version that includes an examples application. There is source code and configuration for some of the Servlet 3.0 features, like annotations.
Have a look at those examples - they're useful.

How to call a local service in mvc portlet?

I have created a my-services-portlet in which i have AbcService, which I am calling from my templates like this
#set ($VeloToolsService = $serviceLocator.findService('my-services-portlet, 'com.mycompany.services.AbcServiceLocalService'))
#set ($article = $AbcService.getArticle($list))
$journalContentUtil.getContent($article.groupId, $article.articleId, 'view', $themeDisplay.language-id, $xmlRequest)
Now due to new requirement, i need to call the same service from another custom mvc portlet. I am not able to the service handle for the service. what is the proper way to get the service handle? so that i can call my existing service from the portlet.
copy the AbcService-service.jar file from first portlet to tomcat/lib/ext folder, restart liferay, and you get access to the service classes.
See http://www.liferay.com/de/community/forums/-/message_boards/message/4585610 the answer from Mika Koivisto.

Session aware spring bean

Is there any way to define a spring bean which will be notified when data in session has changed?
I would also like to know pure java solution if possible. All I want is when i add/edit/delete data in httpsession then I want one java class to be notified to do some processing on that data.
Thanks
You don't need Spring for this, the Servlet API provides that out-of-the-box, via the HttpSessionAttributeListener interface:
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
You declare it as a <listener> in your web.xml file. See here for an example.
In don't know of a specific Spring-friendly way of doing this, though, I think you'll have to use the above listener approach, and notify your Spring beans from there.

function call after the deployement of the app

I wanna know if there is a function that is called directly after the deployment of the server. This functions could be called as an initialisation function of the deployment. Thanks
If this is for a single app, you can implement a ServletContextListener, listening on the contextInitialized() event. You can register an implementation through the <listener> tag in your web.xml.

Categories