I have one webserver with 2 instances of tomcat running. On each tomcat instance I have multiple web apps or web services.
What is the best way to call a function (or trigger some event with parameters) from a webapp of the first tomcat server on a webapp running on the second tomcat server. If it's for example a call using a url with parameters then this call should be secure and not accessible from outside the server.
I've read something about getting the servlet context but is this possible on different tomcat instances? Im thinking that this is only possible with webapps running in the same instance.
I dont want to use CORBA, RMI or SOAP because this is a bit oversized for my problem ... that is what Im thinking :)
Code examples are welcome. Thank you!
The ServletContext is only valid within the same container and can't be shared between two JVMs. The simplest method to do what you're asking is to just use some variety of RPC between the two containers, and RMI doesn't seem like particular overkill. The other usual approach would be a simple HTTP Web service (note the lowercase "s") that invokes your logic in the receiving container.
Spring's HTTPInvoker is great for this. You can use a Java interface, and your code on each instance doesn't need to know the call is remote - it just calls Java methods.
For security, you can use the Sun HTTP server on a different port (instead of using a servlet within Tomcat) and listen only on localhost.
Have a look here
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/remoting.html#remoting-httpinvoker
Use Simple REST services , not that much secured .
Related
I have a Spring application with two controllers. I want to run one controller on localhost:8080 and second controller on localhost:8081.
Am I able to configure Tomcat to serve two ports simultaneously i.e 8080 and 8081? Is it possible? How?
Please note that it is not a Spring Boot application.
It sounds like two completely different applications.
You certainly could configure your Tomcat's server.xml file to have multiple HTTP connectors running on different ports. But you'll find it much easier and hassle-free to deal with two different Tomcat instances.
The App Server (Tomcat, JBoss,Glassfish) run on / watch one port. You can run multiple app servers on a single node (computer) with different port numbers for this reason. They could be the same (Tomcat+Tomcat) or different ones as well (Tomcat+Glassfish)
But in this case you need to split the controllers into 2 different applications and deploy them on the app server instances.
This is the MicroServices architectural desing style. When you run a separate app server for every service. Microservices services most of the cases use REST over HTTP to communicate to each other.
But in case of Tomcat (maybe not by all of the products) it is possible : Running Tomcat server on two different ports
No. spring runs on a specific port and that will be port for both rest controllers . You can have different URLS for them though.
It's not possible.
Spring MVC, as many other web frameworks, is designed around the front
controller pattern where a central Servlet, the DispatcherServlet,
provides a shared algorithm for request processing, while actual work
is performed by configurable delegate components.
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html
Spring itself doesnot run on any port. It is just a technology to create APIs. Port binds with server (like Tomcat, JBoss, etc). So if you want to use different ports for different controllers, then you need to deploy multiple applications across multiple servers and make those servers listen different ports.
On the application that should be on 8081, in the application.properties file add the following line:
server.port=8081
Then Just run both of them...
Otherwise in the TomcatConfiguration set the port to 8081, and again run both of them.
You can find the perfect example in below link. They use different port for different resources. It uses port binding with embedded tomcat in spring boot. Hope this helps you.
https://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html
Yes, you can, but they will behave like two separate applications and are independent of each other. However they can share common resources like databases, Password directories etc.
However for a use case such as this I would recommend to look into microservices.
Read more about microservices here
One approach is to create additional org.apache.catalina.connector.Connector and route requests from it with org.springframework.web.servlet.mvc.condition.RequestCondition https://stackoverflow.com/a/69397870/6166627
I've got 2 different web applications running.
'Web application A' has all the business logic layer services.
I need to invoke methods defined in 'Web application A' from 'Web application B'.
How can I use services(or methods) from 'Web application B' without sending
a http request ?
So far I've found that you can invoke a java programme as described in this question.
Execute a Java program from our Java program
I wonder which one is the right way. Call method by sending a http request or call some method directly. Could you also tell me which one is better, and why ?
Edit
Could I use Springframework's remote invoking ?
http://static.springsource.org/spring/docs/2.0.x/reference/remoting.html
Which one is more desirable. Spring remoting or EJB ?
We are using Tomcat.
Remote EJB invocation is the standard way to make a call between two applications in the Java EE world.
I thing RESTful Web Services is the right way of doing this. You will just expose some of your classes and method as a service and some other application may call them whenever it wants. See this very basic tutorial from Oracle.
currently i have an web app build with Strus2 and Spring (IoC, Transactions), and i want to split this into 2 apps; one client which will contain only the web part and one core services that will be accessed via webservices and/or rmi.
I have a dilemma in what technology should i use for "glue", because i like the fact that webservices can be accessed by any clients (php,.net,...,mobile), but as i understand java rmi is faster then webservices.
I was thinking to expose the functionality via webservices and rmi in the same time... but i do not know how to do that.
Also in my current app i have a ajax action that is executed each second from the client to the server, and in this new configuration i think that there will be some performance penalties due to this.
How should i "attack" this situation ?
Thanks,
but as i understand java rmi is faster then webservices.
Why do you think this? Do you have a citation to bolster this claim?
Both RMI and webservices are using TCP/IP; both incur similar network latency. The former uses Java or CORBA serialization to send messages over the wire; the latter uses either HTTP (for REST) or XML over HTTP (for SOAP or RPC-XML).
The relative speed is far more dependent on what those services are doing and how you code them.
I would prefer a web service because simple and open win. You are restricted to RMI/CORBA clients if you use RMI.
Nice. You are running Spring and you already have all you need. Just throw in a few jars (spring webservices and related jars) and you should be good to go.
Please refer :
http://static.springsource.org/spring/docs/2.5.4/reference/ejb.html
http://static.springsource.org/spring/docs/2.5.4/reference/remoting.html
I am migrating a Java RMI application to Java Web Service (school assignment) and I've encountered an issue...
Currently my Java Server creates an instance of the Remote Object, this object has a constructor and takes a parameter (int ID) which tells it which database to load in memory - works like a charm ...
Now, migrating this to Web Services is causing my a problem - first I needed to add a default constructor because it wouldn't deploy without it, and then while doing some reading all these discussions about "stateless web services" kept coming up ...
For example, if I "start" my webservice with parameter(0) it would load from Databse 0 and all requests from Clients would be done using that data... I want this to only happen when I start the WebService and NOT everytime the client connects... Loading from the DB is expensive and takes time, so I want to do it once so that clients when they connect just deal with the data in memory ...
This is how it works with my Java RMI .... but can this also work with Web Services?
Any advice would be much appreciated.
Thanks,
Perhaps you should consider splitting your presentation from the backing service. Consider the WebService simply a presentation layer and the database processing the service being presented. The web service should really just be referencing the database via a handle to a single instance rather than actually being that instance. This means that the "state" is then held in the database instance not the web service.
One way to consider this (although I would not suggest that you implement it this way) is to consider the Web Service as the RMI client of the database rather than the RMI server service.
Hope this helps.
Kind regards,
Malcolm
I have a problem with web services. They are programed in Java and are running on a WASCE server ( both are on the same server).
My problem that i want to solve:
We have two Web services: App1 and App2
In App1 i want to call a function that is in App2. How can i do this? Is this even possible?
I tried creating a soapClient inside the App1 so i can connect to the App2 but that doesn't work.
exp:
I have a client that calls app1 gets data from app1 and send it to app2 then get back the response data from app2 and send it to an other function into the app1.
What i want to do is to skip the client part and do it directly so that app1 can send directly the data to the app2 and then receive an answer do whatever it needs to do.
For the note: Both of the web services use the connection to the database.
Thank you in advance.
(it has been edited with additional data)
What does "doesn't work" mean? Exactly what happens?
Start by generating some client code for App2. Can you use that from some simple Java environment, or say a Servlet. If that works, what happens when you try to call it from inside your App 1 Service implementation code?
However: if these are related services running in the same JVM can you not set up some simpler relationship using java libraries. My preferred way of developing a service is first to develop some useful Java code, and make sure that works, then "wrap" it as a Web Service. In which case I have a callable routine that can just be invoked as Java.
It's definitely possible, with differing levels of complexity and feasibility depending on exactly what it is you want, and the restrictions you place on it.
Probably one of the simplest ways to go about this, if you don't have a problem with the method in App2 being public, is to simply create a web service exposing that method and call if from App1.
If you want App2's method to be essentially "protected", so that it can be called by App1 but not by public clients, then there are several alternative options. Firstly, you could use firewalls or equivalent to prevent external requests to the service URL. Alternatively, you could expose the method through some form of interprocess communication; RMI would be the obvious native one for Java (set up an RMI method in App2 and export this through a manager, then obtain the reference in App1 and invoke the method remotely). Depending on exactly what it is you want to do, you may be better off with a framework that does all this under the covers; e.g. distributed objects through something like Terracotta.
You should give more detail in your question, though - currently the only thing you've really specified is that you want to call "a function" in App2 from App1. There are dozens (if not hundreds) of ways to go about this and the best one(s) will depend on the details of what you're trying to do.
EDIT (in light of comments): It's not the details of what you want to do that are lacking - I understand fine that you want to call some method in App2 from within App1. It's more the architectural details - what languages are both clients coded in, what libraries are you using to do the web services, are both clients on the same machine or separate ones (and if same machine, same JVM or not), are there any firewall issues that could inhibit certain kinds of connectivity, are there any office-political restrictions that could inhibit your options, are there any security restrictions that could do the same (such as whether you can expose the functionality of App2's method publically or not). All of these will shape what is possible and what is optimal - because at the end of the day, all networking is basically I want to use resources on that remote computer from here. Without more architectural specifics, there are literally dozens of ways that you could achieve this.
Regarding exposition: You would create a web service to expose App2's function in the same way you would create any other web service (with the details being dependent on the tool/framework you're using). As an example if you're using a tool that supports the JSR-181 annotations, you'd write a method in App2 that performs this function, and annotate it with #WebMethod. Then you'd ensure that if this method is not part of an existing webservice class you'd annotate its class with #WebService. I was presuming that since you already have a couple of web services, you'd know how to write/define them.
As for accessing the web service from App1, this can be done quite simply by a Java SOAP client. A tool such as WSDL2Java can create a stub class modelling the remote service that you can call; alternatively you can get a richer interface with something like CXF.
What WS library are you using currently, and what errors have you encountered when trying to use it to perform this interaction?