We are using an embedded jetty instance that we pass a .war file to host a web application. That works very well.
Now, we want to call methods in the hosting java application (that started the jetty server)
What is the best way to call methods in the hosting java application from e.g. servlets inside the webapp without having to use TCP-IP connections, etc?
Is there an easy way?
Thanks in advance
One possible way is to share interfaces so that your web application is built against the interfaces and to use JNDI to obtain the implementing objects from the server.
On server side you only have to publish the implementing instances via JNDI.
Related
I want to access to remote EJB running on Liberty Server from a swing Client like this :
Object found = new InitialContext().lookup(
"corbaloc:iiop:localhost:2809#ejb/ear/ejb.jar/package/Bean#com.package.BeanRemote);
I'm getting that error :
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial
at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:691)
at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at java.naming/javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:342)
at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
at com.package.ClassMain.main(ClassMain.java:43)
How is a connection to Liberty established?
Need help please
I'd strongly recommend you to re-architect your app to use JAX-RS (rest over http) instead of remote EJB. Just use Facade pattern and wrap your EJB as REST endpoints, and use Rest client on the Swing side.
There are several reasons:
remote EJB is almost dead technology now
http/rest is more firewall/network/container friendly
http/rest is more client agnostic - if you would later like to change your client from Swing to web/JS it will be much easier
But if you really insist, setting up remote EJB is not very simple.
The easiest would be to use Liberty Application client and run your Swing app from there.
If you cannot use that you will need to get traditional WebSphere Application Server Client, and use the client jar provided with that com.ibm.ws.ejb.thinclient_8.5.0.jar. If the client is running with a non-IBM java you will also need to add the orb jar onto the class path (com.ibm.ws.orb_8.5.0.jar).
See theses links:
how-to-set-up-thin-client-to-call-remote-ejb-on-liberty
Running the IBM Thin Client for Enterprise JavaBeans (EJB)
Using enterprise JavaBeans with remote interfaces on Liberty
So all in all, rewrite will be usually simpler and more beneficial.
I am new to applications deployments in web servers altogether. Is it OK to add different instances of jetty webserver for two application - One data service and another angular UI application. Or Do I need to deploy the two applications from the same jetty instance.
Suggestions
Single jetty server hosting both applications
Use this approach when you own both the services and understand the RPS/throughout/latency/memory requirements of both the services. A bottleneck in one service can cause issue to another service
If the available memory/cpu/capacity is limited and hence don't want to waste additional memory for server by running another server instance
Both services are light weight
Both services are not deployed frequently or there is proper a BCP
Separate jetty server per application - preferably containerized(Docker?)
Provides good isolation to services
Control over resources per application
Easy to manage/scale independently depending on load
Easy to identify and fix issues
Personally, i would prefer to run them independently with or without containerization.
I have a web app built in GWT running in tomcat that accesses the database. On the same server, I have some server-side Java applications running that need to use the same database connection. These applications will be deployed as a war file alongside the GWT app.
Is there a way I can send the connection from the GWT app to these server-side apps?
This isn't a whole lot of information, I understand. To me, the problem is easy enough to understand, though I haven't found a solution yet. If you need anymore information, I'll be happy to provide.
Do you mean, that the applications have to use the same jdbc connection string or is it more like a shared connection pool.
Is it a solution for you to have a datasource in the local jndi and use this in your applications as well as in the servlets?
Actually this has nothing to do with GWT.
Have a look at this answer
ServletContext provides a way of sharing objects among different servlets and even among diffrenet webapps
this.getServletConfig().getServletContext().setAttribute("sharedObject", shared);
this.getServletConfig().getServletContext().getAttribute("sharedObject");
You cannot use the same connection in two different web apps. But if you mean to use same connection pool form which both the GWT and the other web app will fetch connections, then you need to configure the datasource at the application server level using JNDI
There are lots of JNDI tutorials using servlet in the internet.
Following is a link I found on using JNDI in GWT
http://humblecode.blogspot.in/2009/05/gwt-16-using-jndi-datasource.html
I want to develop a standalone java application, with web browser as front end. This application will run locally and won't be making any remote server calls. I'm essentially using java, as web-browser cannot perform file operations.
I want this application to be portable: no need of installation. Just copying a folder should be enough. I want to know how it can be done, how will javascript communicate with java code.
In continuation of #Quentin's answer.
Yes, you need web server.
There are 2 principal architectures:
Create stand alone application with embedded web server
Create ordinary web application and run it on proprietary web server.
IMHO I think that the second approach is better, however it strongly depend on your application functionality.
You can take jetty or grizzly as a web container. Both can run as in embedded or stand alone modes. You are welcome to share other details of your application with the community if you need concrete advises concerning to the design of your application.
The application would need to implement an HTTP server. Then all communication would be done over HTTP.
Write a small web application as you need and Deploy it using Jetty. Jetty is a pure Java-based HTTP server and Java Servlet container. You can use it by embedded mode also.
Deployment is so easy if you use Jetty-Runner
java -jar jetty-runner.jar my.war
You don't need a local web server. Take JavaFX (embedded webkit) and implement a URL protocol handler for say "myprotocol". Then you can access it from the browser using something myprotocol://xxx.yyy.zzz
I am a little confused about the roles of a java application server and its differences from a web server.
I found many sites explaining the same difference between the two but not to my satisfaction.
So please explain me about the two following cases:-
1)App. Server and its difference with web server:
From these two links:
Difference between an application server and a servlet container?
What is the difference between application server and web server?
web server: It handles everything through http protocol by accepting requests from clients and sending
responses to them with the help of its servlet container(e.g Apache Tomcat)
App. Server: An application server supports the whole of JavaEE like JMS,JPA,RPC etc.
Now what I am confused with is that how can I use a lot of JavaEE APIs like JMS,JPA etc. with my Tomcat
by adding their jar files in my web application ?
Does that mean that if I use an appliation server I don't have to add those jar files?(I don't think so)
2)The roles of an appl. server (This is very important to me)
From Wikipedia
http://en.wikipedia.org/wiki/Application_Server
An application server provides services such as security,transaction support etc.
"The term is often used for web servers which support the JavaEE" -- It sounds like if we add the required jar files of JavaEE APIs a web server becomes an appl. server.What about it.
Now my question is how an application server performs the tasks of security control or transaction management by itself ?
E.g. in my web application using Spring framework I am providing security by using spring-security and transaction management by using #Transactional annotation and all those things you know.
So does the appl. server have anything to do with my security or transaction management or it has its own ways ?
Forgive my ignorance.
Using Spring, you're in fact embedding some kind of Java EE container inside your application. But even when using Spring, if you need JTA support (because you need distributed XA transactions), you'll need to use an additional transaction manager. If you need JMS, you'll need to install an additional JMS broker. If you need connection pooling, you'll need to use an additional connection pool. Sometimes it's as simple as adding additional jars to the classpath and properties or XML files. Sometimes it's harder.
A Java EE app server comes with everything bundled. You have less flexibility, but you don't need to install, configure and make everything work by yourself.
When you use the Java EE framework, that is a specification. So the application server, if it is Java EE compliant, needs to implement this. So once it is implemented the specification, then it will address Security,transaction etc because it is mentioned in the spec. So it is a contract. Whereas, in a web server, it will just pull out your static resource. There is no need for handling other stuff.
In case of the Spring framework, the framework knows how to handle transaction, security etc. So particularly the developer need not look into these aspects which are implemented by the Application Server in the other scenario.
how an application server performs the tasks of security control or transaction management by itself
It is rather the specification that address these issues, not the application server. So, the duty of the app server is to implement these.
So, if your application is Java EE compliant, then these areas will be addressed and the implementation would have been done by the app server.
May be this is oversimplification,
A web server is basically a HTTP server serving contents over http protocol. So a web server is simply about serving the contents over http protocol. A typical example would be Apache web server. This is simply a file server.
Now the question is where does the web server gets the contents from ? Possible sources are
Static contents (the contents like images/css etc) which are not generated on request but statically served.
Dynamic contents: Simply put, the contents to be served are generated upon the user request.
For the static contents, the web server does not need anything as it simply reads the file and serves it.
For dynamic contents, the web server might need help of additional components which will generate the contents to be served.
Here the Application Server comes into picture.
Now these additional components referred earlier, might interact with database or some other system etc.
In a web environment where your website is exposed to huge number of users (intended/unintended), you need typical services like transaction/security/concurrency etc. so that the user get expected responses and do not see inconsistencies in the behavior of the application.
An application server has inbuilt abilities to manage transaction/security/concurrency/resource management. generally these are referred as Managed services and environment offered by them is called Managed Environment where these basic services are managed by the application server and programmer does not have be bother for them.
Application Server needs web servers or we can say Web servers use Application server's services to generate dynamic contents.
For example, JBoss uses Tomcat as inbuilt web server. Whereas web logic has its own web server. Tomcat again can be called as application server (in principle) as it also offers managed environment for servlets (it manages concurrency and instance pool of servlets/JSPs ).
Coming your your example of Spring:
An Application server will come inbuilt with transaction/security etc whether you need it or not. The Spring offers a very nice way handling this. Spring has all these things BUT you use what you need. Not just these, but just a Java Web Sever like Tomcat is sufficient to build a full fledged services that needs an application server.