NServiceBus and Java integration - java

We have several microservices, most of them written in C#, but one written in Java. We're using NServiceBus and RabbitMQ for communication between the .NET services. We have one case where one of the C# Components needs to talk to the Java component with a Request/Reply over RabbitMQ.
When using NServiceBus the reply Queue is labeled with AnyQueueName-1 where the -1 indicates that this is a Reply Queue.
Debugging the Java service, I can see that I'm able to send Messages from the C# Service to the Java service. Once the Java component is finished processing the Message, it's supposed to send back a Message. Here is where all the problems starts.
I can see that it's supposed to send the Message to the correct Queue. The name has been verified within the RabbitMQ Management Tool. But there is not activitiy in this Queue.
No exceptions are thrown on the Java side, which to me sounds like its able to send the message, but where does it go? I do not expect that NServiceBus and Java will work together perfectly, but at least I should see some activity in my expected queue.
The relevant Java-code looks like this:
Message responseMessage = rabbitTemplate.getMessageConverter().toMessage(response, responseProperties);
String replyTo = requestProperties.getReplyTo();
rabbitTemplate.convertAndSend(replyTo, responseMessage, cd);
Where the variable replyTo is the name of the queue that I expect the message to be pushed to.
Can anyone give me a push in the correct direction? Where should I debug next?
Thanks!

This issue is hard to identify without actuall access to the infrastructure.
It seems to me, considering that your Java client successfully publishes to the RabbitMQ broker, but the C# client doesn't react to the messages, that your message serialisation might be missing something.
A few things that you should check:
Take a look at the native integration with RabbitMQ sample
The NServiceBus documentation has a section on how to use native RabbitMQ integration.
You can download the sample and see how the particular guys got it to work.
NServiceBus uses specific header attributes within messages.
One lf these is the NServiceBus.EnclosedMessageTypes, which alows NServiceBus to identify the message type, more specifically to map it to the corresponding C# class implementing IMessage. Also the NServiceBus.MessageIntent header would be needed.
I suggest you check the NServiceBus Message Headers documentation, in your case specially the Reply Headers section. Make sure you add the NServiceBus required headers from your Java client as explained in the RabbitMQ API-Guide. I can't tell which of those headers are mandatory for NServiceBus to work, so trial and error is your friend.
Track Messages published by NServiceBus in RabbitMQ
Compare the messages published using NServiceBus with the ones you publish using your Java client. Here is a link for one possible solution on how to trace RabbitMQ messages and their payload. This way you might identify differences between the two message types and their content.
Use NServiceBus ServiceControl
One of the advantages of NServiceBus is the tools it brings with it. Set up an instance of ServiceControl and use the ServiceInsights to check messages and errors per endpoints. You might find your Java messages listened.
Generally this tooling is great for production environments and I can only recommend it.
If nothing gets it to work, another option for you would be to create a slim REST API in C#, as a publisher gateway, that would take the POST requests and publish the content using NServiceBus to your RabbitMQ. You would be able to reference your project containing your messages and use them as your models for the API.
A possible example:
POST https://busgateway.corporate.com/api/{endpoint}/send/{messagetype}
body:
{
"property": "value",
"publisher": "java client"
}
That way your Java client would send messages using http requests.

Related

Webhook implementation | Doubts?

There is a client to my server, which is calling a GET API to know all updates regarding a particular thing very frequently (let's say once every 5 seconds). Due to this there is unnecessary network calls are landing on my server even if I don't an an update to share.
I reached a decision to replace above approach with web-hooks, where I'll call there POST API whenever I have an Update to share instead of letting them put unnecessary load on my server.
What I understand about Web-hooks:
A web-hook is like a reverse API which POSTs the updates to the server (The client which was calling our application frequently to know/GET updates).
My client has to implement a Web-hook client, which is basically a POST API, I'll be calling whenever there's an event/update to be shared.
I need to call their POST API using REST template wherever there's a new event.
What I don't understand:
Is there anything called a Webhook server? If yes, how do I create/implement it? Any references?
Is it just a reverse API, or is there anything special which both the server and the client needs to handle?
Webhooks are usually used to notify other service that some event occurred on your side. This is made by standard HTTP request sent to some URL.
There is no specific thing like Webhook Server. This is standard application that sends request to someone proactively.
Should both services communicating with webhooks handle something special - this depends on your architecture, there is no "standard" here. You can implement any sort of retry or mechanism to validate that the other side received the information. Im most cases it is assumed that webhooks should be idempotent, so if two same webhooks are sent, the other side should not repeat their action.

Connect to an MQ with Jmeter, and provide a return address

Following on from this, the requirements have evolved. No longer is it enough to send a message to the mq. I must now include a "return address" in the message. I'm given to understand that this will include some information on an MQ I control (but which, annoyingly enough, I haven't been given access to just yet). My question is: what information will I need from 'my' MQ, and how do I include it in the message I send, so that the MQ on the other side will send a dummy response to the correct return address?
As per Return routing IBMMQ knowledge base article:
Messages can contain a return address in the form of the name of a queue and queue manager. This return address form can be used in both a distributed-queuing environment and a clustering environment.
This address is normally specified by the application that creates the message. It can be modified by any application that then handles the message, including user exit applications.
Irrespective of the source of this address, any application handling the message might choose to use this address for returning answer, status, or report messages to the originating application.
The way these response messages are routed is not different from the way the original message is routed. You need to be aware that the message flows you create to other queue managers need corresponding return flows.
IBMMQ message format is not specified (apart from very generic types), it is totally up to developer so you need to ask your developers regarding what you should use as the return address, or if you have a requirement to test responses routing - you should know the queue manager and the queue name yourself.
If you don't have an IBMMQ instance to play with you can easily get one up and running using i.e. IBMMQ Docker Container, check out Testing “Docked” IBM MQ with JMeter - Learn How article for comprehensive configuration instructions.

Send "MOTD" to Minecraft client from custom Netty server

I have a basic Netty server (From the tutorial) (http://netty.io/wiki/user-guide-for-4.x.html), and it recieves the requests from the client, but how would I go about sending a string to the client?
For example, on a normal Minecraft server, you specify the "MOTD" in the configuration file, and when a client pings it from the server list, it will display that string. I need to do the same thing, but from my server code.
If you wish to send the MoTD to the client you will have to figure out what gets sent in terms of protocol and data.
For example in the most basic form the data sent could be 1 byte for action (display motd) and then variable length for a string.
If I had to find out how to send this I would go look at the open-source bukkit repositories or the Minecraft Decompiled Code Repository to find out the way to do it myself.
Update:
Upon looking at the code it seems that also Minecraft uses Netty, so this plays in your advantage in terms of understanding it. Unfortunately the code is unofficially decompiled and thus obfuscated.
Update 2:
I believe the class you should inspect is
net.minecraft.server.PacketStatusOutServerInfo
and the data sent appears to be JSON generated by the ServerPing class.
You can also check out Minecraft Protocol (specifically Ping); a place where modders can find an explanation of protocol & encryption.
This handler shows the sending of the MOTD:
https://github.com/Bukkit/mc-dev/blob/c1627dc9cc7505581993eb0fa15597cb36e94244/net/minecraft/server/LegacyPingHandler.java
It just happens to be that the MOTD handling goes on line number 69: https://github.com/Bukkit/mc-dev/blob/c1627dc9cc7505581993eb0fa15597cb36e94244/net/minecraft/server/LegacyPingHandler.java/#L69
When the channel receives the ping packet, it encodes the response into a ByteBuf and writes it back out of the channel.
Note that there are a few decompilation errors on the file - ignore them and fix it.

Asynchronous websocket mechanism which pretend to be synchronous

I am not looking specific answer, just an idea or a tip.
I have following problem:
Android application is a client for web service. It has a thread, which sends events (XML form with request ID) through http protocol and for every request server sends confirmation, that he understand message right with granted event ID - server is a synchronizer for few clients. I want use websocket protocol to send events through websocket too but it is a little bit tricky - instead of http, I don't expect to get response for every request. Moreover, incoming websocket messages is parsed in other thread. Primary mechanism it's a little bit overgrown and I don't want to write everything from scratch.
I want to make this asynchronous websocket mechanism to pretend to be synchronous.
There is my idea for now - after send event through websocket I will wait no more for e.g 5 seconds for response which will processed in other thread (it's came as XML) and regarding too request ID it will notify proper paused thread. I worry Condition.await() and condition.signal isn't the best idea, what do you think?
According to this problem, I've realized that I have problems with project this kind of mechanism. Do you have an idea, where can I find information about good pattern and tips which good to now to avoid bad approach? Thanks in advance!
The only difference between websocket and HTTP requests is the lack of HTTP headers when a message comes in. In websocket, you have a heartbeat that keeps the connection alive and allows full duplex communication, and then you have pure payloads. It's your job to find which message headers you will use to route the requests properly in your server/client.
So, that doesn't stop you from communicating in a request/response manner by simply writing to the output stream right after receiving. I suggest you take a look at the RFC
https://www.rfc-editor.org/rfc/rfc6455
If you're a little more visual, this slideshow can help:
http://www.slideshare.net/sergialmar/websockets-with-spring-4
Or if you want some more serious implementations as an example, take a look at spring's docs:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

SOAP web service callback architecture?

I am quite new to web services, JAX-WS etc. so maybe noob question...
So, I want to implement a web service to make two systems communicate. The "client" system is interested in events that are generated on the "server" system. But the "client system" is itself a server for a different app. The server is Java (WAR in tomcat). The client is .Net.
There should be just one client system, but several client processes inside the client system, each interested in distinct categories of events.
I will implement the server-side, and a test client. Somebody else will implement the .Net code.
The running sequence should be along this line :
Server is running...
Client initiates conversation, "registers" to the server, and requests some initial data.
Server keeps a list of registered clients' endpoints
In the server there is a listener that is notified when certain events happen. It will then go through the list of registered clients and forwards the event to each of them
At some point, the client can "unregister" no notify the server that it doesn't want to receive events any more.
First, does it sound like something reasonably doable ?
And is there a standard built-in mechanism, using SOAP (JAX-WS on the server, whatever is available with .Net n the client) - that the server can use to obtain a callback endpoint from the client ?
For example, I did something very similar using RMI, in this case the client can just send a remote reference to itself, that the server can just store ant refer to later.
Finally, is there a standard library to store endpoints references, make (collective) callbacks, and maybe keep the list up-to-date, removing the clients that don't respond so some "ping" call ?
Note for clarity : I need more than just asynchronous method with callback: one message from the client will generate many callback messages from server to client.
Seems you wish to implement a notification facility to inform arbitrary anonymous clients.
I suggest you first consider how you would pass the information using SOAP messages. Then you can consider how to achieve this using java - JAX-WS or additional non-standard libraries. The point is there may be significant limitations or assumptions required to transfer SOAP messages. E.g. firewalls might block your HTTP messages, clients might "just clients" with no ability to act in a server role to recieve SOAP notification requests
Note: An async callback mechanism is defined in JAX-WS 2.0, where the service obtains the endpoint reference of the client. This is the same sort of functionality provided by WebLogic/Fusion proprietary solution described by Deepak Bala. Websphere has a similar proprietary async solution. None of these meet your requirements, because they only allow a single response per request.
SOAP Options:
Proprietary SOAP messages - the "100% Do-It-Yourself Option"
You design full SOAP payload schemas and message exchange pattern.
You can push the notification from the server to the client if you know the client's SOAP endpoint address. The client can transfer it's SOAP endpoint address within original SOAP request payload. Sometime later the server can send a SOAP request to the client.
Problems/Assumptions: (1) need a SOAP/HTTP communication path for requests from server-to-client - not guaranteed when firewalls exist; (2) the client needs to be aware of your notification schema - in fact the client needs to act as a Service endpoint to recieve the SOAP notification request. That's two big assumptions IF you are trying to support arbitrary anonymous clients - that's not something SOAP "just supports" both ends need to design all this in detail. In fact to do this in a service typesafe manner, the client should actually declare it's own service WSDL interface so that you can invoke it. (3) As hinted earlier, many clients are "just clients" - they might not have a HTTP server to accept SOAP requests.
So for proprietary "push" notifications to work, both sides need to servers and both need to publish their SOA interfaces.
Alternatively, you can pull the notification to the client. The client can use a notification request to the server that is either blocking or polling. The server can respond with the notification or nothing or error.
Problems/Assumptions: (1) HTTP servers (i.e. the SOAP server) do not support blocking requests, as a rule, meaning you must poll; (2) the client needs to be aware of your notification schema - in fact the client needs to act as a Service endpoint to recieve the SOAP notification request. That's two very big assumptions for an arbitrary anonymous client - that's not something SOAP "just supports" both ends need to design all this in detail. In fact to do this in a service typesafe manner, the client should actually declare it's own service WSDL interface so that you can invoke it.
Same as above, but make include WS-addressing data in SOAP headers to inform either side of the other's endpoint address.
Basically the same Problems/Assumptions as the first option. WS-addressing addresses will help you intelligently route SOAP messages to the right URL address, but no more.
Use WS-notification
This spec was designed for your scenario.
WS-BaseNotification sub-standard would meet your needs. It provides WSDL port definitions for notification producers and consumers. It provides a WS- standards compliant solution for subscription from a consumer to a producer, and a notification from producers to consumers.
Problems/Limitations: (1) It does NOT define the format of notification payloads. The Notification payload is application-domain specific (proprietary design). The standard does not define any “standard” or “built-in” notification situations or messages. (2) It has the same problems with HTTP notifications passing through firewalls as mentioned above.
(3) WS-Notification is not a supported standard of Java EE/JAX-WS (but there are plenty of app servers, open source and commercial, that support it as an extension).
Use a message queuing solution (e.g. JMS) with traffic encapsulated in HTTP
This requires proprietary design of payloads passing between client and server and back - forming contracts between the two sides. An advantage is that the client can be a pure client, with a message listener invoked in a thread when a message is recieved.
Problems/Limitations: (1) It has the same problems with HTTP notifications passing through firewalls as mentioned above. (2) Is a do-it-yourself implementation. (3) Uses more technology then you currently use.
End Result:
You need at least part of your solution to be a proprietary design - the SOAP/WS standards do not meet your full requirements. In theory this proprietary design could leverage a product to provide much of the legwork, BUT the notification schema design would need to be created and integrated by you.
IF you wish to push notifications, you need some sort of contract for notifications passing to the client, the client needs to act as a SOA server, and you need the firewalls openned for your traffic. Most corporations disallow HTTP requests leaving a server and passing to a client - you normally need an extremely good reason to open firewall ports and even then, many corporations will disallow it...
IF you wish to have clients polling for notifications, you just need a basic WSDL interface on the server side that can be called frequently by clients.
Future Option: HTML5 Web Sockets
IF your client is a HTML5 app, then web sockets enabled servers can push traffic to the browser - and there is some chance corporations will open firewalls. SOAP messages could travel over HTTP web sockets, enabling you to push notifications.
Async clients are supported for WSDL based services through the use of polling and callbacks. In your case I think the requirement is relatively more complicated.
The Oracle fusion middleware doc page has a scenario outlined that will help you. It details a method that allows clients to send requests which generate a HTTP 202 (accepted) and the clients then wait for a response on message queues. In your case the scenario can be tweaked from the one shown below.
Initiate several response queues for each category of callback. The clients can filter them by supplying a client and category ID for the queue. This will serve as a callback mechanism for each client or the processes that are governed under each client. The MDB can be backed by a file store or DB store to ensure reliability and one-time delivery.
Of course you do not need Oracle fusionware to implement this. You can use RabbitMQ or Redis (with transactions) to acknowledge receipt of a message on the client. If your client wishes to un-register it make a call and stop listening to the queue.
I'm unaware of any industry standard that will fit your scenario, but I believe this solution should work well for you.
Have you considered the simpler approach of "pub-sub" using a messaging product ?
(Such as MQ, EMS, or ActiveMQ)
The requirements you describe does not seem to fit "classic" request/reply sync/async SOAP Web Service scenarios.
In a Pub/Sub solution, the client(s) subscribe to a topic once, and the publisher(s) (in your case, the Server) can post any number of relevant messages to all subscribers.
As a bonus, most messaging products include support for "durable subscribers", so the client can be off-line at times and receive all messages after re-connection.
In your case, the server could house a (free) ActiveMQ Server... Providing most of the feature you seem to seek.
If you go that way, I suggest you pick a JMS compliant product with support for .Net.
For those getting here from search engines:
You can use a WebHook for Web APIs nowadays, which works essentially as OP described.
In REST for example, the client will have an HTTP endpoint itself that is dedicated to receiving POST events/notifications from the actual server. The client registers his endpoint with the actual server by giving it an URI on his notification endpoint. From that point on, the actual server can POST to the client’s notification endpoint. It is essentially a convoluted callback, if you are familiar with async terminology.
Maybe Java or .Net have libraries for WebHooks by now.
That said, SSE and Websockets standards provide actual push and real-time messages while being compatible with HTTP and most browsers.
Long polling variations were also used in the past, and are now sometimes called Comet as an aggregate.

Categories