Integrate Spring-based Java application and Clojure library - spring

We have a Spring 4.0-based web application running in Tomcat 8 (alternatively we have a start-up script for Undertow). Spring MVC is handling requests. I am looking for a way to defer some of request handling code to Clojure library, with minimal changes to legacy Java code.
For instance, requests with URLs ending with .java would be handled by legacy Java, and requests ending with .clj would be handled by Clojure. For now, i see three options:
Include Clojure library jar in Java project's dependencies and use clojure.java.api to invoke Clojure code from Java.
Use some sort of RPC/RMI or message queues, e.g. Redis and Carmine's message queue processing capabilities. This way, Clojure would live in a separate JVM.
Use some sort of reverse proxy to perform URL routing.
Are above approaches actually feasible? What else would you suggest?
Thanks!

I've done a similar thing but on a DropWizard application rather than a Spring application. I went with approach #1.
I followed the example here - https://stackoverflow.com/a/2187427/827617
to create the library. That way you don't need to use clojure.java.api, your Clojure library compiles down into a jar that you can include in your Spring application and call directly from Java (the functions that you expose are static methods on a class).

Related

How do I tell Camunda (in Modeller) v7.15.0 that Java Delegates are remotely deployed?

I have Camunda 7.15.0 deployed in GCP Cloud Run. I have a Java 11 application through which I wish to access the Camunda RuntimeService in order to start a process instance, but this requires some community extension so I went with Camunda REST API. Now I want to add Java Delegates coded in the above Java application, how do I tell Camunda (Modeller) that Java Delegates are remote?
The Java Delegates are only working if you embed Camunda in your application as library.
In your case you need to work with the External Task pattern.
Here is a Blog, that explains this Pattern: https://medium.com/#dashedsouvik/camunda-external-task-pattern-fd84a29d9d3e
(I assume you are asking about Camunda 7.x)
If the code is supposed to run in a different JVM, change the model to use implementation type "external". Then change the JavaDelegate. You do not need to implement this interface anymore. Instead, for Camunda 7 use one of the client libraries:
https://github.com/camunda-community-hub/awesome-camunda-external-clients
The libraries take care of the remote communication to the engine. You only need to change the delegate code to be wrapped similar to this: https://github.com/rob2universe/c7-rest-task-worker/blob/main/src/main/java/com/camunda/example/worker/RESTWorker.java
Also see:
https://docs.camunda.org/manual/7.17/user-guide/ext-client/
https://blog.bernd-ruecker.com/moving-from-embedded-to-remote-workflow-engines-8472992cc371

Dynamic Camel route configuration at deployment time: Java DSL or XML DSL?

Let me preface this with the fact that I am still very new to Apache Camel. I'm still trying to understand how it all works, and what needs to be done (and HOW to do it) to achieve a particular effect.
I am trying to develop a Spring Boot application that will use Apache Camel to handle the transmission (and possibly also receipt) of data to/from a number of possible sources and destinations. The purpose of the application is to provide a means to produce/generate network traffic, at the network application level, that will be fed into another Spring Boot application - let's call this the target. We are trying to observe and measure the effects various network loads have on the target.
We would like to be able to transmit data via a number of protocols, including: ftp, http/s, file systems (nfs), various mail protocols (smtp, pop) and data streaming protocols for voice and video. There may be other protocols added at a later time. The data itself is irrelevant, we just need to be able to transmit data via various protocols with various loads.
These applications/services will be running in a containerized environment (Docker) that will be run within our local development and test environment, as well as possibly in a cloud environment, such as AWS. We have used Docker, Ansible, Terraform and are currently working towards using Kubernetes and Istio to manage the configuration, deployment, and operation of these applications.
We need to be able to provide specific configurations of Camel routes for particular deployments.
It would appear that the preferred method to configure Camel routes is via Java DSL, rather than XML DSL. The Camel documentation and nearly every other source of information I've found have a strong bias towards using Java DSL. Examples of XML DSL route configuration are far and few.
My initial impression is that going the Java DSL route (excuse the pun), would not work well with our need to be able to deploy a Camel application with a specific route configuration. It seems like you are required to have Java DSL defined route configurations hardwired into the code.
We think that it will be easier to provide a specific route configuration via an XML file that can be included in a deployment, hence why I've been trying to investigate and experiment with XML DSL. Perhaps we are mistaken in this regard.
My question to the community is: Considering what I've described above, can the Java DSL approach be used to meet the requirements as I've described them? Can we use Java DSL in a way that allows for dynamic route configuration? Keep in mind we would not be attempting to change configuration during operation, just in the course of performing a deployment.
If Java DSL could be used for this purpose, it would be very much appreciated if pointers to documentation, examples, etc. could be provided.
For your use cases you could use XML DSL also. Anyhow below book covers most aspects Camel development with examples. In this book authors describes XML DSL use for most of java DSL examples.
https://www.manning.com/books/camel-in-action-second-edition
In below github repository you can find the source code for all the examples listed in above book.
https://github.com/camelinaction/camelinaction2
Simple tutorial and github repository for Apache Camel using Spring boot.
https://www.baeldung.com/apache-camel-spring-boot
https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-camel
Maven Plugin for build and deployment of spring boot container application into Kubernetes cluster
https://maven.fabric8.io/
In case if your company can afford some funding for your effort look at below link which provides commercial offerings around Camel.
https://camel.apache.org/manual/latest/commercial-camel-offerings.html
Thanks
Madhu Gupta
Our team has a few projects which use the Java DSL for building routes. In order to make them dynamic, there are control structures for iterating and setting endpoints based off configurations. That works for us because the routes are basically all the same, just with different sources and sinks.
If you could dynamically add/change the XML DSL files in a way that doesn't involve redeploying your application, that might be a viable route to follow. One might, for example, change the camel.springboot.xml-routes property to point to a folder which changes as needed.

GWAN as embeded server for spring boot

not sure if this is possible but was wondering if its possible to embed GWAN in spring boot app (like tomcat/jetty etc.) are. and how so?
As far as the Spring Boot documentation tells, the requirements are to package your G-WAN application as a jar (zip) file, and install and configure Spring plugins to connect the dots.
One of them, Gradle, takes 50.5 MB to let you use several programming languages: "Gradle makes the impossible possible, the possible easy and the easy elegant."
G-WAN delivers even more language mix in 200 KB of code. G-WAN works without configuration files, nor it needs any war archives to be built, and deployed each time you edit the code of a Java G-WAN script. G-WAN's philosophy is real-time "edit & play".
With some adjustments, it will be possible to use G-WAN with in a Spring Boot application, but thats' like trying to attach a lorry trailer to a formula one: are the expected gains worth the exercise?
If you see advantages at adding this much code, configuration and maintenance overhead to G-WAN, could you please try to enumerate them?
If you are an advanced Spring developer and you still believe that there's value in this proposition, we will help you by adapting G-WAN wherever needed, providing you or other Spring experts can tell what must be done.

Using spring in a stand alone jar for dependency injectection

Am a total beginner with spring framework and trying to know if it even fits my use case, before investing time learning it.
I'm responsible for a stand alone java project(used as a jar by a server) which basically serves requests from a server, and in turn makes service calls to various internal services.
This standalone java project, currently has all of its service calls hard coded. I want to use Spring to inject dependencies so I can make this stuff testable.
I have no idea how spring works. Does it even hold for standalone jars or is it only for 'running applications'?
If I make my standalone project 'spring enabled', when the server uses my jar, will it automagically work by creating beans or is there some requirement from the server side?
In short, yes, you can use Spring in a standalone jar-application ("console application", if you will), we do it all the time at work. You just need to create the ApplicationContext yourself when your application starts, see for example here: http://www.devdaily.com/blog/post/java/load-spring-application-context-file-java-swing-application
This is just one example I pulled straight out of Google, there are probably numerous others. Still, you really need to read at least the basics from the Spring documentation to get started, otherwise you'll probably hit a wall pretty soon.

GWT with spring dependency injection

I googled but couldn't find any answer.
I am planning to use GWT. I want to know if I can use spring in my GWT code to use the dependency injection framework? I am not talking about GWT gui interaction with backend spring app.
The reason I am asking is the GWT code gets compiled to JavaScript and this is what gets executed in browser. If I am using spring code in that, then would it work or for that matter any other library like log4j, etc.?
Or the GUI code have to be pure GWT API only?
For example,
public class MyTable {
private Button myButton;
#Autowired
public MyTable(Button aMyButton) {
myButton = aMyButton;
}
}
Guice is supported on GWT using GIN. For Spring-like DI with GWT, check out GWT Toolbox or Rocket GWT.
I believe GIN is a more natural choice for GWT. Not because it's also made by Google, but because using XML for GWT configuration makes absolutely no sense. Everything gets statically compiled into JavaScript so there is no need for externalized configuration. Keep your refactoring tools happy; go for GIN.
To answer your other question, you will not find many SE frameworks that work on GWT. First and foremost, it has no support for reflection or bytecode manipulation (everything is JavaScript), which immediately rules out a lot of frameworks. Log4j, on the other hand, doesn't make sense because there is no file system accessible on the client side, but there are libraries available that do things differently.
The Spring libraries for GWT mentioned above are basically a rewrite of the Spring for GWT. They do not share any code with Spring simply because they can't. Those frameworks work by generating code ("factories") that wire up your components as if you were doing DI manually.
This is also how GIN works, it generates Java factories for your classes, and GWT compiles it into optimized JavaScript (meaning little performance overhead). GIN does use Guice behind the scenes though, to validate configuration at compile time and to inspect modules.
No, you won't be able to do that. The DI logic applies at runtime on the server side, and the GWT code is entirely client-side.
I thought it would be simpler to just create a Spring Controller that invoked the doPost method of the GWT RemoteServlet. A sample is provided here. I know this is a little round about. But this shields you from changes to the GWT implementation if any.. Hope it helps.
I wonder if Guice (the Google DI framework) is supported by GWT?
This might be an alternative.
You can implement a Servlet Service in the server side that retrive objects from a Spring ApplicationContext, rendering to JSon Objects (I did it with http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONSerializer.html) by example.
Then you can have a Singleton Facade Service that make the request from the GWT-client side to our Servlet Service.
In this way you can get a runtime depency injection in the GWT-client side .
Spring ME is capable of helping you out here. Although I partly agree with some of the previous responses, it's nice to have the same programming (and plumbing) paradigm across your client and server code.

Resources