开发者

Web Service and multiple requests from the same client

开发者 https://www.devze.com 2022-12-09 13:55 出处:网络
If I have a client app sending requests to my web service, one after another, will the web service be able to handle each request made and not override previous request because of a new request made?

If I have a client app sending requests to my web service, one after another, will the web service be able to handle each request made and not override previous request because of a new request made? I want all requ开发者_运维技巧ests to be handled and not replaced for another. Will I be able to do this with the multiple requests all coming from the same client


I have no idea why the other answer is so long to what is essentially a simple question about the basics but the answer is yes.

Each request is independent of others, unless you specifically program some sort of crossover into the server (e.g. a static cross-thread list used by every request or a more complex structure).

It is easier to encounter crossover on the client side, if using an asynchronous pattern that gives results via events - you need to make sure you got the result to the correct request (generally done by providing some token as the "custom state" variable, which you can use to determine the original request in the response handler).


The answer depends on your architecture.

For example, if the server is multi-threaded, and the business logic part is stateless, then on the server the requests won't override, as each thread will call a function and return the result.

On the client side, your best bet is to have each request sent from a different thread, so that that thread blocks until it gets its response, then the processing can go on fine.

If you have a different design, please describe it.

UPDATE: Based on the new info here is something you may want to look at: http://weblogs.java.net/blog/2006/02/01/can-i-call-you-back-asynchronous-web-services

I am curious how, or if, you are doing asynchronous webservice calls. Generally webservices seem to block, but if you are making these calls so fast then I can only assume asynchronicity.

So, the webservice can store the answers on the server-side, so there is a stateful class that stores results in a dictionary, by IP address. The client then polls for answers, so, ideally, if you send a request, you should be able to get back an array of answers as a response. If you have sent all the requests and are still waiting for more responses, then poll. You should be able, again, to get an array of answers, to cut down on wasted bandwidth.

The better approach is to have your client also be a server, so that you send the request, with the IP address:port for the callback, so the server would make a oneway response to the client. But, this is more complicated, but it cuts down on wasting bandwidth.

Update 2: This is being done without checking it, so there is probably errors:

@WebMethod
public ResponseModel[] AnswerQuestion(QuestionModel[] question) {
// Get the IP address of the client
  AnswerController ac = new AnswerController(question, ipaddress);
  return mypackage.myclass.StaticAnswers.GetAnswers(ipaddress);
  // return an array
}

@WebMethod
public ResponseModel[] GetAnswers() {
   return mypackage.myclass.StaticAnswers.GetAnswers(ipaddress);
}

OK, this should give a rough idea.

There is no assumptions in AnswerController. It should know everything it needs to do the job, as it will be stateless, so, it refers to no global variables that can change, only const and perhaps static variables.

The StaticAnswers class is static and just stores answers, with the lookup being ipaddress, for speed.

It will return the answers in an appropriate array.

When you have sent the last question then just call GetAnswers until you have gotten back everything. You may need to keep track of how many have been sent, and how many have been received, on the client side.

This isn't ideal, and is a very rough sketch, but hopefully it will give you something to work with.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号