The documentation says that when spring cloud config server detects configuration chages it fires an RefreshRemoteApplicationEvent. But documentation said nothing about how that event is handled. So is it true that each application which receive such event shoud handle it by itself? E.g it is not required to refresh entire Spring context when such event was received?
I think the documentation only talks about the server side, i.e. the Spring application that talks to the git repository and exposes the condensed information to interested clients. In this process, for example using webhooks, the server can be informed about changes in the git repository, and in turn sends out events to applications that might need to be re-configured.
Your question seems to be concerned about the client side. If your application uses Spring Cloud Config, it should automatically request the new configuration data as soon as the event described above arrives at the client. This in turn should mean that the new configuration values are available or some configured behaviour (log level?) changes.
To actually make the server fire an event that arrives at the client, the documentation suggests Spring Cloud Bus. If you create (for example) a RabbitMQ instance, and make this available to both your clients and your server, Spring automatically attaches to this system and is able to process messages. Additionally, the Spring Cloud Config server automatically sends the desired events using this system, and the clients automatically process these.
In short, if you add Spring Cloud Bus to all involved applications (and make the system used by it, e.g. RabbitMQ, available to them), everything works as expected.
Related
I have been learning how to use spring framework and so far so good. I have created an app from Spring Initializr - http://start.spring.io/ and i have been able to setup numerous controllers and even setup security and everything works out.
I am running my app on Apache Tomcat. I now want to use Spring AMQP and the way i want to use it is like this. I shall execute a controller method in the browser and that adds a message in the queue which should be consumed by a consumer,possibly a threadpool powered consumer and return the result to the user.
For instance i want to add a message to the queue and return json to the user.
Since i executed the add message from a controller, i am expecting to show the response to the user.
After going through the rabbit mq docs, i think RPC would help me publish the message to the queue and it shall be consumed and the result returned to teh user. I setup the rabbit mq example using php and i saw it work.
However, upon looking through the spring amqp docs,i came across this three
-Message Listener https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#containerAttributes
-Listener Concurrency https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#listener-concurrency
-Request/Reply Messaging https://docs.spring.io/spring-amqp/docs/1.3.5.RELEASE/reference/html/amqp.html#request-reply
-RPC https://www.rabbitmq.com/tutorials/tutorial-six-spring-amqp.html
My question is,if i wanted to let the user get the result, would i pick RPC or Request/Reply Messaging or are they the same thing?
Second, in the last RPC example i did, i started the listener separately like java_listener.java to be able to consume the messages.
Since i started my app with spring intilizr and i am running tomcat, will i need to start the consumer separately from the tomcat process?.
Thanks.
I have Apache ActiveMQ embedded into my java 8 server side project. Its working fine, and I am able to send and consume messages from pre-configured queues. I now need to be able programatically remove messages from the queue upon request. After reading some docs I found that Apache ActiveMQ has a sub-project called Artemis that seems to provide the required functionality. But I am a bit confused on how to do it. Is Artemis sort of plugin on top of ActiveMQ and I just need to add required dependencies and use the tools or is it a separate product and it doesn't work with Active MQ but as an independent product. If so how do I manage individual messages (in particular delete requested message) in Active MQ?
First off, 'ActiveMQ Artemis' is a sub-project within the ActiveMQ project that represents an entirely new broker with a radically different underlying architecture than the main ActiveMQ broker. You would run one or the other.
To manage messages in the ActiveMQ broker you would use the JMX Mamagement API and the Queue#remove methods it exposes to remove specific messages. This can be done using the Message ID or more broadly using a message selector to capture more than one message if need be. The JMX API is also exposed via Jolokia so that you can manage the broker via simple REST calls instead of the JMX way if you prefer.
In any case this sort of message level management on the broker is a bit of an anti-pattern in the messaging world. If you find yourself needing to treat the broker as a database then you should ask yourself why you aren't using a database since a broker is not a database. Often you will run into many more issues trying to manage your messages this way as opposed to just putting them into a database.
I'm creating a monitor application that monitors the activities of a user. There are four elements in my system:
EventCatcher: The EventCatcher is responsible for catching all the events that happen in a subsystem and pushes the data to the EventHandler. Based from observation, there is an average of 10 events per second that is being pushed to the EventHandler. Some events are UserLogin, UserLogout.
EventHandler: The EventHandler is a singleton class that handles all the incoming events from the EventCatcher. It also keeps track of all the logged in users in the system. So, whenever the EventHandler receives a UserLogin event, the User object is extracted from the event and is stored in a HashMap. When a UserLogout event is received, that User object will be remove from the HashMap. This class also maintains a Set of all active Websocket sessions because everytime an event has occurred, I would want to inform all the open sessions that a particular event happened.
Websocket Endpoint: This is just a simple Java class annotated with #ServerEndpoint.
Clients: The system I will be building is for internal (company) use only. At production, at most, there will only be around 5 - 10 clients. All the clients will be receiving the same information every time an event has occurred.
So right now I am trying to convince my supervisor that Websockets is the way to go, however, my supervisor finds it really unnecessary because a simple polling solution would do the trick.
His points are:
We don't really need up-to-date information by the millisecond. We can poll every second.
If I was to maintain a list of open WebSocket sessions, how would that work in a clustered environment (we use a load balancer)
If I plan to send information to the client every time an event (UserLogin, UserLogout) has occurred, I should be able to just send small updates to all WebSocket sessions - meaning, I can't be sending a whole JSON dump of everything. So that means, for every WebSocket instance, I would have to maintain another Set of Users and properly maintain it to mirror the Set contained in the EventHandler.
What my supervisor suggests is that I lose the WebSocket and just convert it to a simple Servlet and let the clients poll every second to receive the entire JSON dump.
In this scenario, should I stick with WebSockets? Or should I just poll?
The main advantage, as far as I've read, of Websockets vs. polling is that by using Websockets, you will have a persistent connection from client to server. HTTP is not really meant for real-time data.
Also, polling requires sending an HTTP request every time and every request comes with HTTP headers. If an HTTP request header contains 800 bytes, then that's 48kb sent per minute per client. With a WebSocket, this isn't problem.
But then again, we won't really have a lot of active clients. We're not concerned about third parties sniffing our requests because this system is for company use only - internal use! And I believe my supervisor wants something simple and reliable.
I am fine with either way. I just want to be sure whether I'm using the right tool for the job.
Additional question: If WebSockets is the way to go, is there any reason why I should consider polling?
The entire purpose of WebSocket is to efficiently support continuing connections between client and server.
I’m not clear on how you are implementing your app. If this is a web app running in a Servlet environment leveraging WebSocket support in the web server, be aware that you need to use recent versions of the Servlet container. For example, with Tomcat you must use either version 8 or the latest updates to version 7.
And of course the web browser must have support for WebSocket.
Be aware that WebSocket is still a new technology that has been changing and evolving in both the specs and the implementations.
Atmosphere
You may want to consider using the Atmosphere framework. Atmosphere supports multiple techniques of Push including WebSocket & Comet.
The Vaadin web-app framework leverages Atmosphere to provide automatic support for Push in your app. By default, WebSocket is automatically attempted first. If WebSocket is not available, Vaadin+Atmosphere falls back automatically to the other techniques including polling.
For my Spring-based web application, I now have the requirement to send out weekly e-mails to my application's users.
What are elegant solutions to this requirements?
Up until now, I have come up with the following possible solutions:
a dedicated cron job that I schedule to run once a week, running independently from my web application JVM process and outside of the web application Servlet container. This process takes care of sending out those weekly e-mails. To accomplish sending personalized e-mails, it reuses domain classes (such as my User class) that I have already developed for my web application. This dedicated process accesses my application's MySQL database concurrently to the running Spring Web MVC servlet?
a scheduled mechanism inside my Spring Web MVC servlet or inside my Servlet container.
In this setup, the e-mail sending happens inside the same JVM and the same servlet container as my web-serving Spring Web MVC servlet. Maybe this setup has (irrelevant?) advantages such as "database connection pool sharing" and "transaction sharing" "class sharing" with the servlet hosted inside the same environment.
Using or not using Spring Batch, for any of the above conceived setups. I have no experience right now with Spring Batch as to judge whether Spring Batch is or isn't an adequate tool for my requirement.
Maybe there are other solutions as well?
I am especially interested in answers that can give insights and guide in making an educated decision.
It is irrelevant for this particular question whether e-mails get sent with my own infrastructure or with a third party e-mail SaaS service.
From your description, the code for generating newsleters must share common code base with your main application. So the natural solution is to develop this code withing your main application. The open case is how this code is triggered:
From CRON. You start a script from CRON that would trigger the function within you application somehow. This somehow may be a process listening on specific port, or, what is quite natural for web application, a dedicated URL that would trigger newsletter. Just make sure that URL can't be run from outside, only from localhost (check caller IP, for example). You must, however, deal with the situation, that your app is down (restarting for example) when CRON launches the script.
From within the application. For example, using Quartz. The minus is that you need to include new library, create database tables for Quartz. The plus - Quartz will handle situation, when the task was scheduled on the moment when the application was down, because it stores the information about what was launched in DB.
We always use cron to fire a JMS message to a queue and have a dedicated process which consumes these messages. You can add the email contents to the message or just use the message as a trigger. The nice thing about this approach is you can fire in a JMS message from anywhere and have multiple handlers lots of different email scenarios. The only downside is installing a JMS broker, if you don't already have one...
I am building a Spring-MVC based web application which is required to send a weekly newsletter to a small group of people. I am using Spring's built-in scheduling mechanism. http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html
Yes, in this setup, the e-mail sending happens inside the same JVM and the same servlet container and it is quite easy and handy to implement the solution. I am observing the stability and reliability of this mechanism and cannot feedback more about it now.
I have a system which contains the data of the employee and this system has exposed many web-services and RMI through which any other system can request for data. Now I have a web application hosted on JBOSS. Here the problem is, now I want to get the data from the system to JBOSS in real-time fashion. Though that system has sevral webservice and RMI services through which JBOSS/web-applications can request data but that is on-demand. I looking for a way thorugh which if there is any change in the system, JBOSS get notify at that moment. One solution to it is, I should make process which will call the webservice to know if there is any change in the employee data within a time-interval. What is the other way to notify the JBOSS in real-time?
One way is to have a job/separate process which polls your employee application periodically, and sends "data has changed events" to JBoss using JMS.
In JBoss you could have a message driven bean (MDB) whichs listens to these events, and stores them in a database.
Another possibility is to have this job running in JBoss which just stores the events/results in memory (search for Quartz, for example have a look at this tutorial).