How to run a thread inside a webservice - java

Could someone help me on this,
I have created simple web services using axis2 , apache and tomcat. This web service has a queue that keeps xml files sent from a client, so whenever a client calls a method on the webservice, the webservice loads this xml to its queue. Now I want to have a thread running in the webservice which monitors this queue, and if there are items in the queue takes some action.
But my problem is that the webservice is invoked only if a client calls one of the methods on its interface. But I need this thread to be running on webserbice. Could someone tell me is there are is a way to do this?
When a client invokes a method on the webservice, it does not consider previous method invocations. This means that it does not keep track of data in the queue, for each invocation it creates new queue.

If you need background threads in a web application you must manage them inside a ContextListener registered in web.xml. You are then notified when your web application is started and stopped.

Sounds like the perfect use case for JMS/message driven beans. Spring JMS provides these facilities without having to use a full-blown J2EE container, so tomcat will fit here. Active MQ can provide the messaging engine.
Essentially, your web service would put a message on a queue and a message driven bean (or message driven pojo) would read them off the queue and process. Using JMS would have the advantage that you'd be able to reconfigure the message driven bean to sit on a separate host if you're load on the server grows. It'll also mean you'll be able to move to different app servers with ease as JMS is a standardised solution.

Related

Spring AMQP RPC vs Spring AMQP Request/Reply Messaging

I have been learning how to use spring framework and so far so good. I have created an app from Spring Initializr - http://start.spring.io/ and i have been able to setup numerous controllers and even setup security and everything works out.
I am running my app on Apache Tomcat. I now want to use Spring AMQP and the way i want to use it is like this. I shall execute a controller method in the browser and that adds a message in the queue which should be consumed by a consumer,possibly a threadpool powered consumer and return the result to the user.
For instance i want to add a message to the queue and return json to the user.
Since i executed the add message from a controller, i am expecting to show the response to the user.
After going through the rabbit mq docs, i think RPC would help me publish the message to the queue and it shall be consumed and the result returned to teh user. I setup the rabbit mq example using php and i saw it work.
However, upon looking through the spring amqp docs,i came across this three
-Message Listener https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#containerAttributes
-Listener Concurrency https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#listener-concurrency
-Request/Reply Messaging https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#request-reply
-RPC https://www.rabbitmq.com/tutorials/tutorial-six-spring-amqp.html
My question is,if i wanted to let the user get the result, would i pick RPC or Request/Reply Messaging or are they the same thing?
Second, in the last RPC example i did, i started the listener separately like java_listener.java to be able to consume the messages.
Since i started my app with spring intilizr and i am running tomcat, will i need to start the consumer separately from the tomcat process?.
Thanks.

Web service implementation Java EE

I have web service cloud system based on Java EE. I use jboss as application server, java version 1.7
My system is something like a bridge between client and merchant servers. Client sends data via soap protocol (amount USD, merchantID, paymenttype and so on.) and i make service implementation based on requested params and send back to client.
I have a question:
There are two ways to make core implementation,
1 is to run it in main thread, every web service runs in main thread
2 per service call should be in separate thread.
I don't want code examples or anything, just what is the correct way to determine my solution?
You can use the jboss request processing thread pool to control it. There does not seem to be a reason to attempt custom thread pool. Its a a complete different discussion if you are thinking about asynchronous communication.
I got the below from another SO post which is helpful
http://www.mastertheboss.com/jboss-server/jboss-performance/jboss-as-7-performance-tuning?start=3
In my case, I designed my system as multiple threads managed by main thread. This solution is very helpful and easy to maintain.

Integrating weekly e-mail delivery/newsletter with Spring Framework

For my Spring-based web application, I now have the requirement to send out weekly e-mails to my application's users.
What are elegant solutions to this requirements?
Up until now, I have come up with the following possible solutions:
a dedicated cron job that I schedule to run once a week, running independently from my web application JVM process and outside of the web application Servlet container. This process takes care of sending out those weekly e-mails. To accomplish sending personalized e-mails, it reuses domain classes (such as my User class) that I have already developed for my web application. This dedicated process accesses my application's MySQL database concurrently to the running Spring Web MVC servlet?
a scheduled mechanism inside my Spring Web MVC servlet or inside my Servlet container.
In this setup, the e-mail sending happens inside the same JVM and the same servlet container as my web-serving Spring Web MVC servlet. Maybe this setup has (irrelevant?) advantages such as "database connection pool sharing" and "transaction sharing" "class sharing" with the servlet hosted inside the same environment.
Using or not using Spring Batch, for any of the above conceived setups. I have no experience right now with Spring Batch as to judge whether Spring Batch is or isn't an adequate tool for my requirement.
Maybe there are other solutions as well?
I am especially interested in answers that can give insights and guide in making an educated decision.
It is irrelevant for this particular question whether e-mails get sent with my own infrastructure or with a third party e-mail SaaS service.
From your description, the code for generating newsleters must share common code base with your main application. So the natural solution is to develop this code withing your main application. The open case is how this code is triggered:
From CRON. You start a script from CRON that would trigger the function within you application somehow. This somehow may be a process listening on specific port, or, what is quite natural for web application, a dedicated URL that would trigger newsletter. Just make sure that URL can't be run from outside, only from localhost (check caller IP, for example). You must, however, deal with the situation, that your app is down (restarting for example) when CRON launches the script.
From within the application. For example, using Quartz. The minus is that you need to include new library, create database tables for Quartz. The plus - Quartz will handle situation, when the task was scheduled on the moment when the application was down, because it stores the information about what was launched in DB.
We always use cron to fire a JMS message to a queue and have a dedicated process which consumes these messages. You can add the email contents to the message or just use the message as a trigger. The nice thing about this approach is you can fire in a JMS message from anywhere and have multiple handlers lots of different email scenarios. The only downside is installing a JMS broker, if you don't already have one...
I am building a Spring-MVC based web application which is required to send a weekly newsletter to a small group of people. I am using Spring's built-in scheduling mechanism. http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html‎
Yes, in this setup, the e-mail sending happens inside the same JVM and the same servlet container and it is quite easy and handy to implement the solution. I am observing the stability and reliability of this mechanism and cannot feedback more about it now.

Is there any way to send the realtime data to JBOSS

I have a system which contains the data of the employee and this system has exposed many web-services and RMI through which any other system can request for data. Now I have a web application hosted on JBOSS. Here the problem is, now I want to get the data from the system to JBOSS in real-time fashion. Though that system has sevral webservice and RMI services through which JBOSS/web-applications can request data but that is on-demand. I looking for a way thorugh which if there is any change in the system, JBOSS get notify at that moment. One solution to it is, I should make process which will call the webservice to know if there is any change in the employee data within a time-interval. What is the other way to notify the JBOSS in real-time?
One way is to have a job/separate process which polls your employee application periodically, and sends "data has changed events" to JBoss using JMS.
In JBoss you could have a message driven bean (MDB) whichs listens to these events, and stores them in a database.
Another possibility is to have this job running in JBoss which just stores the events/results in memory (search for Quartz, for example have a look at this tutorial).

running a thread inside webservice

Could someone help me on this,
i have created simple webservices and it has a queue that keeps xml files send from a client, so whenever webservice client call a method on webserbice , webserbice load this xml to its queue, now i want to have a thread running on webserbice which monitors this queue if there items on the queue take other actions.
But my problem is , webservice only invoke if client calls one of the methods in ws interface. so but i need this thread to be running on webserbice. so could someone tell me is there are a way to do this
You have to run your web service (server) inside a web application, which means that you probably have a web.xml somewhere. You can create a servlet, and initialize (starting) the processing thread on its init method, stopping it on the destroy method of the servlet. Be aware, thought, that by using this model you may lose unprocessed messages if the web application is stopped. You should perhaps be looking at JMS or something like WebSphere MQ, they both can act as messaging systems - you receive the message, queue it in a different queue, and then when the processing application is ready, it pulls the next message, processes it, and informs the calling process back.

Categories

Resources