How can something like BOSH be implemented using Java Servlets - http

BOSH (Bidirectional-streams Over Synchronous HTTP) is a sneaky way of implementing 2-way client-server communication in situations where true server-push is not allowed, most obviously to let a server push data to a browser client without having to use client polling.
It works by the client sending a request to the server, and the server doesn't respond immediately... rather it remembers the request but only responds when it has some data to send. When this happens the client immediately sends another request so there is virtually always a 'stored request' sitting on the server ready to push data to the client.
At least, that's how I think it works!
Update:
My question is how you can do this using a Java EE stack i.e standard servlets. Is this possible using say Servlet 2.x (I'm a bit rusty so I don't know if you can decline to send a response or something) or only using extensions through a wrapper like Atmosphere?

Not an equivalent but Servlet 3.0 introduces an Asynchronous API. With or without Servlet 3.0, there is also Atmosphere.
See also
Servlet 3.0 Asynchronous API or Atmosphere? Easy decision!
Asynchronous HTTP and Comet architectures
Jean François Arcand blog (the author of Atmosphere)

I think this is what you might be looking for: http://blog.jwchat.org/jhb/

Maybe you are looking for something like comet, a kind of reverse AJAX in which the client initiates the connection, allowing the server to push data when it wants.
EDIT: I realize you are looking for solutions in Java and when we think of AJAX we immediately think of JavaScript, but the term has been tainted lately and it represents a concept more than a JavaScript solution. Comet is very much a concept like AJAX and can also be implemented in the programming language of your choice.

Related

How should a synchronous public api be integrated with message-based services?

I've been reading about microservices, and have found a lot of interesting advice in Jonas Bonér's Reactive Microservices Architecture (available to download free here). He emphasises the need for asynchronous communication between miroservices, but says that APIs for external clients sometimes need to be synchronous (often REST).
I've been trying to think how asynchronous response messages sent back from microservices should best be routed back to the waiting client. To me the most obvious way would be to record something like a request id in all messages sent when processing the request, and then copy this id into response messages sent by the services. The public API would block when processing the request, collecting all expected response messages which have the matching id, before finally sending the response to the client.
Am I on the right lines here? Are there better approaches? Do any frameworks take the work of doing this routing away from the developer (I'm looking at Spring Cloud Streams etc, but others would be interesting too)?
He emphasises the need for asynchronous communication between
miroservices, but says that APIs for external clients sometimes need
to be synchronous (often REST).
When dealing with client - backend communications you can have a couple of types of operations and they should be handled seperetly (look at the idea of CQS):
State changing operations - they should be one way fire and forget using messaging (it can be the client calling an HTTP API and the api dispatching the message)
read operations: synchronous (request response) operations (using an HTTP API) and this is does not involve any messaging what so ever
Does that make sense?

Implementing callback from DB layer into App layer

I have a Java Servlet which writes a message to the database. Some other tier picks up this message from the DB and processes it and updates the status of this message in the database. Meanwhile in the servlet, I have to keep polling the database to get an update on status of the message which was written earlier.
How can I implement a Callback instead of polls so that unnecessary database queries are avoided?
I suppose you're talking about server push technologies. I suggest you to use HTML5 websockets for this. Use your same servlet with a websocket to communicate between both ends.
There are so many examples out there.
Java WebSockets - In this example he uses jetty, but you can
use jboss or tomcat for this.
StackOverFlow post describe the same.
pushing data for multiple clients from a server via websockets
try above links and it is worth for trying.

CXF Asnchronous Service Invocation

I'm looking for an example showing how can I configure my CXF project, so that I can call a service method asynchronously; meaning my invocation in the client side doesn't block for the response and when the response is ready the logic will be done. I'm very thankful if somebody can help me
Best
The simplest way is to use #Oneway annotation on the server side. CXF will handle the request on the server side in a separate thread, so the client won't be blocked and will return immediately after receiving 200 response code and empty response body.
Of course in this case you cannot receive any response (by definition of request-only SOAP operation), which is not an option for you. Unfortunately you're need to implement this by hand using thread pool and future tasks. Fortunately this is very simple since Java 5, start by studying ExecutorService API.

Can an HTTP connection be passed from IIS/ASP.NET to another application or service?

I'm looking into building an ASP.NET MVC application that exposes (other than the usual HTML pages) JSON and XML REST services, as well as Web Sockets.
In a perfect world, I would be able to use the same URLs for the Web Sockets interface as I do for the other services (and determine which data to return by what the user agent requests) but, knowing that IIS wasn't built for persistent connections, I need to know if there's a way that I can accept (and possibly even handshake) the Web Sockets connection and then pass the connection off to another service running on the server.
I do have a workaround in mind if this isn't possible that basically involves using ASP.NET to check for the Web Sockets connection upgrade headers, and responding with a HTTP/1.1 302 Found that points to a different host that has my Web Sockets service configured to directly listen to the appopriate endpoint(s).
If I completely understand your goal, I believe you can use the IIS7/7.5 Application Request Routing module to accomplish this.
Here's a quick reference: http://learn.iis.net/page.aspx/489/using-the-application-request-routing-module/
Rather than 302 responses you could use ISAPI_rewrite to direct to an appropriate endpoint (and manipulate the HTTP header to get it there)
http://www.isapirewrite.com/docs/
Otherwise no, IIS cannot natively pass off an HTTP connection. The current MSFT method is to use a 302 or something else that is intercepting the raw socket and performing header manipulation prior to sending to IIS (or whatever other application)
It strikes me that this would be a better question to ask Microsoft than to ask us. Web Sockets is new technology, and rather than looking for a hack, you might want to ask Microsoft how they plan to support it. IIS is their software. Poke around on http://iis.net (maybe in http://forums.iis.net) and see what you learn.
The way to do this is to use a unique Session ID that is associated with the Http Session. From the description, it seems like you might want to scope this to a single HttpApplication instance, but this is not necessary (you may also persist a session across many application instances). Anyway, this session ID needs to be attached somehow to each Http Request (either with a cookie, querystring, static variable with the HttpApplication instance, form data). Then you persist the identifying information about the Http session somewhere with the ID.
This identifying information may vary depending on your needs but could entail the entire http request or just some stripped down representation that serves your particular purpose.
Using this SessionID somewhere in the Http request allows you to restore whatever information you need to call and interact with the appropriate services. The instances of the services may also need to be scoped to the session as well.
Basically, what I am suggesting is that you NOT directly pass the Http connection to an external process, but instead pass the necessary data to the external process, and allow create a mechanism for sending callback data. I think looking into the mediator pattern may be helpful for you in understanding what I mean here. http://en.wikipedia.org/wiki/Mediator_pattern . I hope this helps.

Making a WCF call with AJAX

Is it required to use a RESTful
service to be able to make a ajax
call to a wcf service (for example: by using
WebInvoke attribute on Operation
contracts)
Once a service is made RESTful by adding a webHttp binding on the service host, can the host have other endpoints as well? (wsHttp or netTcp)
Is it required that the aspNetCompatibilityEnabled be set to true for a service that has webHttp binding (and can this setting coexist for other endpoints)
I understand I can use both JQuery and ScriptManager for making WCF calls on the client. Why should I use one over the other?
Answers
No.
AJAX is typically used for sending simple HTTP GET ("REST") requests. It doesn't have to be so. You could also format a payload using a SOAP envelope, and POST it to the endpoint. In that case the WCF service would have to be wsHttp or basicHttpBinding, at least. Here's an example of using VBScript to create and send a SOAP request, but you could do the same in Javascript. You can't use the more advanced SOAP extensions, like WS-Security, XML DigSig, and so on. Well, you could but it would be impractical. For example, I don't know of any XML canonicalization library in Javascript, which is essential for doing WS-Security or digital signatures. There are 17 similar obstacles. Result: you can't use the more advanced SOAP extensions when calling from Javascript.
.
If you use jQuery ajax, you'll need to use the beforeSend callback on the ajax request to set the SOAPAction header.
.
Having said that, it's a lot easier to process json in a Javascript program, than it is to walk the DOM of an XML document. In other words, you're better off using JSON/REST when connecting from Javascript to WCF, instead of SOAP. Sometimes it's not an option, I guess.
Yes
A WCF service can have multiple endpoints and they can listen on the same or different transports such as HTTP, net.tcp, net.pipe, or net.msmq.
No. aspNetCompatibilityEnabled just enables some ASMX-like features on the server. It affects how the service is designed, and it is independent of the message signature. It does preclude the use of non-HTTP protocols. For more on this, see Wenlong Dong's article.
as for which framework to use on the client - which is easier? I don't have experience with ScriptManager, but the decision criteria is pretty simple. jQuery works just fine, and is appropriate if you already use jQuery. If you don't have or want jQuery, you can use XmlHttpRequest to send SOAP or REST requests. If those are somehow inappropriate, use something else.

Resources