Is it possible to set up AMQP message properties in outgoing message? - spring-rabbit

I am using Spring Cloud Stream with RabbitMQ binder. I need to call external service which does not use Spring Cloud Stream. This service is using type and correlation_id message properties.
I tried to set headers in outgoing message but, even though properties, technically are headers, they are treated in special way. So setting type header does not set a property.
I am aware about interceptors and, if using only Spring RabbitMq, it would not be a problem. But since Spring Cloud Stream represents higher level of abstraction, all binder specific settings are hidden.
Is there any possibility to set up RabbitMQ properties in outgoing stream message?

Properties are mapped from message headers keyed by AmqpHeaders constants; in this case AmqpHeaders.TYPE (amqp_type) and AmqpHeaders.CORRELATION_ID (amqp_correlationId).
All "unknown" message headers are mapped as rabbit headers.

Related

Create a Batch HTTP API With multipart response

Actually, I´ve create a Batch HTTP API that receives a JSON array with many different requests to our backend server. The Batch API just call all of these requests to a load balancer, wait for the return of all of them and return a new JSON to the client.
The client receives a huge JSON array response with its indices in the same position as the request, so it is easy to know what response is addressed for what request.
The motivation for this API was to solve the 5 browser simultaneous connections and improve performance as the Batch API has a much more direct access to the server (we do not have a reverse proxy or a SSL server between then).
The service is running pretty well, but now I have some new requirements as it is gaining more use. First, the service can use a lot of memory as it has a buffer for each request that will only be flushed when all responses are ready (I am using an ordered JSON Array). More, since it can take some time to all requests be delivered, the client will need to wait everything be processed before receiving a single byte.
I am planning change the service to return each response as soon it is available (and solve both issues above). And would like to share and validate my ideas with you:
I will change the response from a JSON response to a multipart response.
The server will include, for every part, the index of the response
The server will flush the response once its available
The client XHR will need to understand multipart content type response and be able to process each part as soon as it is available.
I will create a PoC to validate every step, but at this moment I would like to validate the idea and hear some thoughts about it. Here some doubts I have about the solution:
From what I read, I am in doubbt of that content-type is right for the response. multipart/mixed? multipart/digest?
Can I use an accept request header to identify if the client is able to handle the new service implementation? If so, what is the right accept header for this? My plan is to use the same endpoint but very accept header.
How can I develop a XHR client that is able to process many parts of a single response as soon as they are available? I found some ideias on the Web but I am not entirely confident with then.
I will change the response from a JSON response to a multipart
response.
The server will include, for every part, the index of the
response
The server will flush the response once its available
The
client XHR will need to understand multipart content type response and
be able to process each part as soon as it is available.
The XHR protocol will not support this work flow through a single request from the client. Since XHR relies heavily on the HTTP protocol for communications, XHR follows the HTTP connection rules. The first and most important rule: HTTP connections are always initiated by the client. Another rule: XHR returns the entire content-body or fails.
The implications for your workflow is that each part of the multipart response must be requested individually by the client.
From what I read, I am in doubbt of that content-type is right for the
response. multipart/mixed? multipart/digest?
You should be in doubt as there is no provision in the specfication to do this. The response-type attribute is limited to the empty string (default), "arraybuffer", "blob", "document", "json", and "text". it is possible to set the override MIME type header, but that does not change the response type. Event given that case, the XHR spec is very clear about what it will send back. It is one of the types listed above as documented here.
Can I use an accept
request header to identify if the client is able to handle the new
service implementation? If so, what is the right accept header for
this? My plan is to use the same endpoint but very accept header.
Custom HTTP headers are designed to assist us in telling the server what our capabilities are on the client. This is easily done. it doesn't necessarily have to be in the accept header (as that also is a defined list of MIME types).
How
can I develop a XHR client that is able to process many parts of a
single response as soon as they are available? I found some ideias on
the Web but I am not entirely confident with then.
XHR is processed natively by the client and cannot be overridden for all sorts of security reasons. So this is unlikely to be available as a solution for this reason.
Note: ordinarily one might suggest the use of a custom version of Chromium, but your constraints do not allow for that.

ServiceWorker source headers

navigator.serviceWorker.register('/service-worker.js');
How can I retrieve the /service-worker.js headers without initialising an extra AJAX request?
There's nothing in the service worker spec that provides for this. You can get the url from the registration object, but I don't see anything about headers.
They are planning to add a special expiration header for service workers in the future, so maybe it will eventually make sense to expose headers on the registration as well.
Unless there's a non-service-worker-specific way to do this, I think you're out of luck.

Header Content vs. POST

We are building a POS tablet system between server and android system using android studio. Which one is better in sending json data into the server ? Is it Using POST or header content ? And which one is more reliable ?
I think what you really are asking is this:
When send data from a client to a server via HTTP, should the data of the POST request be in the body or can it just be inside a header?
While you technically can put data in the headers, it's more standard to have the core data of the message should be in the body of the POST. The headers exist to describe the data (e.g. Content-Length, Content-Encoding, etc...) or ancillary data (e.g. authentication token). Also, the headers are typically understood to be readable strings, while the body of the POST can have binary data or alternate encodings (as specified by the headers).

node.js response.writeHead on http module

I'm implementing my own http module.
As I'm reading the official node.js http module api, I couldn't understand a few things:
If the user is using the response.writeHead(statusCode, [reasonPhrase], [headers]) function, are the headers should be written immidiatly to the socket or are they first supposed to be saved as a member to the object? and then written only after .end() function?
What is the meaning of implicit headers that should be used whenever the user didn't use writeHead()? are they supposed to be set ahead? and if the user didn't set them? what should be the behavior? thanks
Answers:
Anything that you write into response either headers with writeHead or body with write is buffered and sent. You see they use socket buffers. They can only hold fixed amount of data, before being sent. The important fact to remember is that you can only set headers before you start writing the body. If you do, some headers will set for you by the http server itself.
Implicit headers are ones which you don't write specifically but are still sent. Setup a simple http server, by responding a request without setting any header. Then view the request headers opening the site in browser. There will be headers like Date, Server, Host etc which are added to every request automatically without user's volition.
I found answer for the first question, but still don't understand the second one.
The first time response.write() is called, it will send the buffered header information and the first body to the client. The second time response.write() is called, Node assumes you're going to be streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body.

HTTP GET request to KDB+ Process using AngularJS

I am working on a web app using AngularJS with which I am quite inexperienced.
My aim is to have the user type some parameters into a text field, and then for a GET request to be sent out (using the $http service in Angular) using that parameter as one of the params in the GET request.
The target of the request is a KDB+ process (A proprietary column oriented database/language from KX Systems). This process can take a request in the form of:
http://servername:1234/?queryToRunHere[paramFromApp]
This call should return a JSON string. Due to the fact that the webserver and this process run on different ports I get the "Access-Control-Allow-Origin" error that prevents me from running this query to a different port. I have attempted to use the $http.jsonp command (since I read this allows remote fetching) however this asks me to provide a "&callback" parameter. This is not possible as KDB+ will interpret everything to the right of the "?" as an internal query.
I am not really sure how to proceed, but these are my ideas so far:
KDB+ provides an API for Java (but not as far as I am aware for JavaScript). Perhaps I could write some sort of web service in Java, which could then be called by angular, and which in turn would call KDB+ using its native API. The issue with this is that it creates another program to maintain, and also I am not sure how to go about writing such a service (what technology, framework, etc).
There is another way inside Angular that I don't know about, or there is a way to not have to specify the callback parameter in $http.jsonp.
I would be grateful any help.
That error is usually only caused by different servers, its in order to prevent cross site scripting, the usual workaround is to add headers in your apache config. make sure both services are referred to with the same host name.
Alternatively the KDB+ response can be customized if you want though it may be tricky if your new to q.
.z.ph is what controls KDB's processing of HTTP requests, you could overwrite it to process any parameters and return whatever you want. An example can be seen on the KX wiki.
KDB+ 3.0 and newer also provide web sockets which you may find more useful. They are handled by .z.ws.
An example of its use in javascript can be seen in Carlos Butlers' WebStudio.
Hopefully these have provided you with workarounds for your issue.
You're getting Access-Control-Allow-Origin error because of the same origin policy built into browsers. By default, you'll get this error when your JavaScript and Server API do not originate from the same server:port. To get around this, you need to specify the Access-Control-Allow-Origin in the header of your HTTP response from the server.
To accomplish this in KDB/Q, do the following:
.z.ph:{ "\r\n" sv ("HTTP/1.1 200 OK"; "Access-Control-Allow-Origin: *";
"Content-Type: application/json"; ""; (.j.j select from aTable)) }

Resources