This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Generic servlet  which can listen on a socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Generic servlet  which can listen on a socket" Watch "Generic servlet  which can listen on a socket" New topic
Author

Generic servlet which can listen on a socket

Ankit Bhandari
Greenhorn

Joined: Dec 23, 2011
Posts: 9
Hi All,
I am very new to the servlet concept. I am trying to write an application which takes input (Non SIP non HTTP) on a socket & then converts it into SIP. Here is a scenario ....

1. My application will be listening on a socket say for example 7080 and will receive message.( Message simply contains To and from user name)
2. A generic servlet will get invoked which will construct a sip message using the caller-callee information sent in a simple string. ( I am not looking for any parsing to be done by container).
3. That generic servlet will then invoke a sip servlet.

Can somebody help me in designing this application.















[Thumbnail for abcd.JPG]

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1189
    
    1

Welcome to Java Ranch !!!

you can write a Generic Servlet which will have 3 methods

a. init()
b. service()
c. destroy()

In init() method, you can write code which listens to port 7080
When ever a message is recieved on port 7080, control goes to service() method where you can construct sip message using the caller-callee information. Then control can be forwarded to SIPServlet with required information
In destroy() method, you can discontinue listening to port 7080 (closing the socket)


~ abhay


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Ankit Bhandari
Greenhorn

Joined: Dec 23, 2011
Posts: 9
Abhay Agarwal wrote:Welcome to Java Ranch !!!

you can write a Generic Servlet which will have 3 methods

a. init()
b. service()
c. destroy()

In init() method, you can write code which listens to port 7080
When ever a message is recieved on port 7080, control goes to service() method where you can construct sip message using the caller-callee information. Then control can be forwarded to SIPServlet with required information
In destroy() method, you can discontinue listening to port 7080 (closing the socket)


~ abhay



Thanks a lot Abhay !!!

I have one doubt regarding the approach you suggested. As per specification of jsr289 and container manual, it says we should not spawn a new thread from servlet, as container manages the thread pool ( Please Correct me if i am wrong.
1. Will it be okay to spawn a thread from init ?
2. The thread that gets spawned through init() is container thread or an independent thread ( How can i verify this by looking at thread signature ...).


Thanks you
Regards,
Ankit Bhandari
Ankit Bhandari
Greenhorn

Joined: Dec 23, 2011
Posts: 9
Hey Abhay,

I have one doubt regarding invoking service method. As you said, "When ever a message is received on port 7080, control goes to service() method " . Can you please help us in doing this. I am not able to invoke service from there.

Thank you
Regards,
Ankit Bhandari
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

service() -Dispatches client requests to the protected service method. There's no need to override this method.


No pain, No gain.
OCJP 1.6
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I'm not convinced that tackling this issue within the confines of the Servlet API is the right approach. The GenericServlet/HttpServlet distinction makes it sound as if every protocol could be handled simply by extending GenericServlet, whereas the API is actually geared very much towards request/response protocols like HTTP. Your problem sounds like it would be better handled by directly listening on a socket, and then making SIP requests from within that socket server.
Ankit Bhandari
Greenhorn

Joined: Dec 23, 2011
Posts: 9
Tim,
Thanks for your reply. As you suggested, will it be okay to invoke a listening socket from container. Or i can make container to listen on a socket and deliver raw buffer to some servlet.


Thank you
Regards,
Ankit Bhandari
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Yes, you can open a ServerSocket from within a servlet container. Or you can create your own server that is independent of it. Not sure what you mean by "deliver to some servlet". Your server process would not be a servlet, no matter whether it runs inside the servlet container, or outside of it.
Ankit Bhandari
Greenhorn

Joined: Dec 23, 2011
Posts: 9
Thanks a lot Tim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic servlet which can listen on a socket