views:

528

answers:

2

I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request. Is the following trail of thought the correct way to implement this: 1. Have a .proto file which is the message definition for the incoming request. 2. Write a servlet which accepts this request, does various tasks like querying database and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response? 3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?

Any suggestion or idea will be very much appreciated.

+4  A: 

Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.

The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.

Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.

I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.

Jon Skeet
Would this be a proper implementation for Protocol Buffer? I am not 100% sure I am new to it. Isn't Protocol Buffer ideal for sending messages from process to process where you control both client and server ends? So having your Web App HTTP --> Protocol Buffer does not make sense to me. sorry
Peter Delaney
@Peter: Servlets don't have to serve HTML. Web services can be implemented with servlets, and they can in turn use protocol buffers. What makes you think the OP *doesn't* control both the client and the server, or that he's not at least in a position to dictate that the client should be written to post/consume protocol buffers.
Jon Skeet
+1  A: 

HI,

I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.

msharma