Create classes into test folder - java

I have two services, the first it's spring boot rest api service , and the second Event proccessor(java).The main logic is , when client make http request to spring boot service it send message to rabbitmq exchange, the second service listening queue that bind to this exchange name.To test this life cycle , I add class from spring boot ,that send message to exchange , into test folder of the second service(Event processor). My question is, is it good practise to test things like this one.

It is always up to you, as in your example, you have service sending a message to the queue and a service which is basically processing queue which is an end to end flow so the test you described also end to end and it is based on the rabbitmq also. I will suggest following to tests instead of one e2e:
(1st service) Emulate client request and make sure the first service is generating expected message
(2nd service) Emulate incoming message and make sure service processing message as it should

Related

Broadcast message to all instances of kubernetes java service

The problem: I have a spring boot service running on K8s. Generally API calls can be served by any pod of my service, but for a particular use case we have a requirement to propagate the call to all instances of the service.
A bit of googling led me to https://discuss.kubernetes.io/t/how-to-broadcast-message-to-all-the-pod/10002 where they suggest using
kubectl get endpoints cache -o yaml
and proceeding from there. This is fine for a human or a CLI environment, but how do I accomplish the same from within my Java service, aside from executing the above command via Process and parsing the output?
Essentially I want a way to do what the above command is doing but in a more java-friendly way.
Seems like your spring boot service should be listening to a message queue, and when one service receives a specific HTTP request message to the /propagateme endpoint, it sends an event to the topic to all other clients listening to the Propagation topic, when the instances receive a message from the topic they perform the specific action
See JMS https://spring.io/guides/gs/messaging-jms/

How to make IBM MQ listener process slowly using spring java application

We are trying to migrate our legacy system to Micro service
With Paas environment, we have scheduler jobs to trigger and put messages in MQ one by one and we have MQ listener in our Microservice to get message and create request and send request to external party.
Here the problem comes our micro service is capable doing Asynchronous call to external service, but our external service is not able to handle Asynchronous call so it is returning wrong data.
For example, we are hitting external service with 40 to 60 request per minute and external service is capable to handle only 6 request per minute.
So how can I make the MQ listener to process slowly.
I have tried reducing setMaxConcurrenceConsumer to 1 and
Used observable.toblocking.single() to make the process to run in only one thread.
We use RxJava in our micro service.
It sounds like either your micro service or the external service is not following the use case for Request-Reply messaging.
(1) Is the external service setting the Reply's message Correlation ID with the Request message's Message ID?
(2) Is your micro service performing an MQGET with the matching option of getting by Correlation ID.
You can blame the external service for the error but if your micro service is actually picking up the wrong message then it is your application's fault. i.e. Does your micro service simply get the "next" message on the queue?
Read this answer: How to match MQ Server reply messages to the correct request
Here's a explanation (looks like from the 90's but has good information): https://www.enterpriseintegrationpatterns.com/patterns/messaging/RequestReplyJmsExample.html
In long term approach we are planning to migrate the External service to as well.
In short time i have fixed it using the observable.toblocking.single() ,thread.sleep(), and setMaxConcurrenceConsumer() to 1 so only one thread will run at a time. which will avoid the Asynchronous call to external service.The sleep time will set dynamically with some analysis done on the external service.

How to 'check' before picking off the Amqp/ RabbitMQ Queue (Spring AMQP)

Scenario:
A microservice picks up a message from a RabbitMQ Queue, it's converted to an object and then the microservice makes a REST call to external service.
It's going to deal with thousands 'n thousands of these messages, is there a way of telling my consumer not to pick up a message off the Queue if we know the external Rest service is down?
I know I can do retries for an individual message once it's picked up, but I dont want to even pick it up if I know its down.
I dont want to deal with thousands of messages in DLQ.
Also it feels like a Circuit Breaker design pattern, but I cant find any specific examples of how to implement it with AMQP.
Extra info:
SpringBoot app, taking to RabbitMQ using spring amqp.
Thanks in advance
You can stop and start the message listener container.
If you are using discrete containers you can stop/start the container bean.
If you are using #RabbitListener, provide an id attribute and wire in the RabbitListenerEndpointRegistry bean.
Then registry.getMessageListenerContainer(myId).stop();.

spring broker channel access

I have a few questions one how to use spring websockets and messaging. So I have a program that interfaces with an external web service producer endpoint that will send data payloads to my web service consumer endpoint. While on the other end of my program I will be routing these data payloads to multiple websocket connections (stomp and sockjs). The external web service producer is providing a subscription ID in each data payload for every query requests so my approach is to send them back to the broker with a SimpMessagingTemplate with it's own unique destination (ie. /user/{subscriptionId}/subscribe). That way I can subscribe each websocket client to an existing destination if a duplicate query was made and only make requests for a new subscripion to the external web service producer if otherwise.
How do I access my SimpMessagingTemplate from within different component such as my web service consumer so that I can send the data payloads to my message broker? Do I just declare my SimpMessagingTemplate static and declare a getter function within my controller where the template object is stored?
How do I get a list of all known destinations and as well as the number of stomp client subscribers to each one? The external web service producer sets a termination time for each subscription, so I would like to implement auto renewal requests if there are still subscribers to a destination. I suppose I can keep track of it myself with Maps/Caches and update them everytime a websocket session is opened or closed, but i prefer to do it with spring if possible as it minimizes my risk and probably less error prone, or perhaps a full featured broker such as RabbitMQ or ActiveMQ is necessary to do this.
Found the answers I needed:
All I need to do is use spring Autowiring support and the bean will be injected with the object initialized
#Autowired
private SimpMessagingTemplate
Need a full featured broker for this, however for what I want to do i decided it would be too much work and essentially not needed. I decided I will just implement my own subscription checking with the 3rd party web service on my own with java maps/caches. I've set went to painstaking lengths by setting breakpoints in eclipse in the java .class files even with a java decompiler plugin and found out that all of this information can be found in the DefaultSubscriberRegistry class. Although I can not access it with the api given by Spring, I can rest assured it is being properly handled by the application. When a client subscribes or disconnects to my application, the information in the internal maps/caches of the registry are added and removed accordingly. Furthermore I can make make changes to my own implemented map/caches by implementing the interfaces provided by Spring such as SessionSubscribeEvent or SessionDisconnectedEvent and sub class it with ApplicationListener and they will be triggered whenever a client subscribes or disconnects.
public class SubscribeEvent implements ApplicationListener

How to identify whether a web service is asynchronous or synchronous

I know that there are two types of web services called asynchronous web services and synchronous web services. What I want to know are
1.
Can I identify whether a web service is asynchronous or synchronous by inspecting the WSDL of the service? Or is there any other way such as inspecting the generated stub etc?
2.
Can I develop an asynchronous client for a synchronous web service? Does it make sense?
Thank you.
I'm not an expert, but I'll give my two cents and hope that you find this useful.
Well I assume that it depends. You can have an asynchronous architecture with no signs of it. Someone could implement the following
The client makes a request to the service.
The service responds with a success or error message just to indicate the status of the request.
The service begins to process the request.
The service makes a request to another endpoint. This request is the actual response for the first request.
The service get a response, that the message was received successfully.
This is an asynchronous architecture. The service (actual service) when finished sends the actual response to an endpoint. This endpoint is actually the client that sent the first request.
This endpoint, the one that the client is listening to, for the actual response can be predefined (hard coded), or can be included as a parameter (callback) in the first response. If the case is the latter, then I guess you could deduce that this is an asynchronous web service.
For the second part of your question. Yes you can. Check this Asynchronous web services calls with JAX-WS: Use wsimport support for asynchrony or roll my own? and this http://cxf.apache.org/docs/asynchronous-client-http-transport.html

Categories