Java Spring MVC deploy .war on GlassFish - java

At first i want to say that i'm a beginner in java servers and never deploy an java .war app on server other than localhost in my intellij.
I have a simple only REST app Which work fine in localhost, i also created war file and deploy it by command "asadmin deploy --port 4849 war_name", but when i go into url http://stachurskipiotr4.usermd.net/cookbook-1.0-SNAPSHOT there is an server error.
I completely don't know what it is, i will be grateful for any help.

Access the domain port for that case. Default is 8080 if you haven't configured.
http://stachurskipiotr4.usermd.net:8080/cookbook-1.0-SNAPSHOT
Also, please use or log in your admin console to see the apps if it's deployed. You can also access it directly from there.

Glassfish already provides the Jackson libraries and the version conflicts with the one Spring depends on. Glassfish is an enterprise container, is there a specific need for the enterprise container? Try a regular servlet container like Tomcat instead. If you do want enterprise, maybe you shouldn't need using Spring
EDIT:
Removing jackson jars from your project won't help you; Spring code wants the version it wants. You can try to find out which version Glassfish provides and then see which version of Spring depends on that and use that version of Spring. This is a wrong thing to try as this will be just the tip of the iceberg. Do not try to deploy Spring code in an enterprise container, these are competing specs.
Your options are
1) figure out why is Tomcat not working. Tomcat is a battle-tested servlet container with thousands of production deployments.
2) Try a different servlet container such as Jetty.
3) Rewrite your code to the JEE specification and deploy to an enterprise container such as Glassfish or Wildfly (there are others).
4) Use Spring Boot to embed the container and package your app as an executable jar.
EDIT2:
If you chose Spring Boot, your artifact will be an executable jar, you do not deploy it into a container since the container is embedded. You start the jar via a java command. You remove the container from your deployment entirely. I don't know your deployment environment, but you do not use Glassfish at all at this point, you have to run the java command to start the server.
If Glassfish is a requirement, you have to rewrite your code

Related

Spring boot embedded container or war file in an external container for production

I'm complete able to configure spring boot in both cases, the question here is which of them are more robust and is the more recommended, because I didn't find in the spring boot documentation the recommended way to deploy it in a production environment, my concerns about use the embedded container are:
If I want to set it as a Windows or Linux service, is the jar file the best option?
If I use the jar file I'm not going to have access to restart the server.
Maybe in the future I need more applications in the same container.
If I restart the machine I have to execute again the java -jar.
The question in general is which is better use the jar file and execute it as java -jar jarname.jar in production or change the packaging to war set the tomcat as provided and set the generated war in an empty tomcat.
I hope you can help me.
---EDIT---
Many times the answer is depends, this is for a normal web application or REST web service.
jar packaging is perfectly suitable for production and you should rather fallback to war only if you really have to - which is often the case when you cannot control your deployment environment (which is often the case in large enterprises).
There is a chapter in Spring Boot Reference about setting up Spring Boot based application as a Unix/Linux/Windows service: Installing Spring Boot applications.
Regarding your concern:
Maybe in the future I need more applications in the same container.
With embedded containers if you need more applications running on the same machine, you should start two applications separately, each running on different port and effectively you will end up with two containers running - which is good, applications are better isolated from each other.
About a month ago I had the question like yours.
Let me share my conclusion:
1) JAR:
You can run independently every appliction with different ports (in linux, java -jar ... > app_logs.log &) and you can route it (e.g. nginx). Note that, restarting is not problem. You can write custom bash script (like this: ps aux | grep appname and kill by PID)
But there are some problems with configuring production app. Property files will archived into jar.
2) WAR
You can deploy into container and just run it. Easy managing at the server. If you want to re-configure app, open properties file from unarchived folder inside container, change it as need and restart container. So, managing and configuring will be easy.
But, if you want to run another app in this server with another port, then you must install another copy of container and config it.
So, in my practice, using war app easier than jar to manage and re-configure.
I don't know that much about Windows services but on Linux you can add the execution of a jar to a RC-Scripts (and thus make the application start at a certain run-level). For a spring boot app you just have to symlink to the jar and you can start/stop/etc like any other service, see: Spring Boot application as a Service
restart the machine or the JVM? A shutdown mechanism is built into spring boot, you just have to activate it (and you should enable security machanism so that not anybody can do that), see: How to shutdown a Spring Boot Application in a correct way?
Spring-Boot enables microservices - so the idea is to have one embedded webapp-container for each webapp/microservice. This reduces the risk of losing all services when only one is going down.
Yes. and you have to execute catalina.sh|bat start after every restart. Or you add an appropriate startup script (see 1.)
I sense that you'd rather do it the old-fashioned way. Despite the 'matter of taste' answer, there is one argument pro-jar: the only dependency is the JVM! The rest (the web-app-container, db-drivers, other libraries) is all part of the package you deliver. And if you decide to change the container for the next release, so will it be.
One more reason to use "war" file in production.
Springboot masked an error Jetty threw whereas WAR deployed in Jetty correctly caught it ( though issue below is still under review )
https://github.com/spring-projects/spring-boot/issues/8917#issuecomment-294673487
I don't know much about server kind of things, But my recommendation is
If you are using Monolithic application, better to use war with
external tomcat.
If you are using for Micro Service applications, use embedded
tomcat with different port. And each micro service applications are
independent from each other.

Is it possible to consume a web service deployed in glassfish by an application on client side using apache tomcat?

this may be two questions in one, so...Sorry, please correct me if I'm wrong.
I have to deploy a web service developed with JPA, JSP and Glassfish 3.1.2, in a machine which only has apache Tomcat 7 as server installed on Windows 7.
I don't really know how apache can connect to the database externally, or if that is really possible if you don't use TomEE.
I know in Glassfish you can have a connection pool and a jdbc resource, and if you're using JPA, that's how the connection to database works(kindof), so you can deploy the .war file. But, if you only have a Tomcat and the app is using JPA, so it does not connect itself to the database(It does not have a class with a connection credentials), How can I achieve this?
Or, Can I deploy the .war of the web service on Glassfish and then be consumed by an application that only works with apache Tomcat on client side? Am I mixing concepts which should not be mixed?
Any enlightenment is highly appreciated.
You can use Tomcat with JPA, you can even use it in a standalone java application. Define a persistence unit name in persistence.xml, make sure the transaction type is RESOURCE_LOCAL (you can't use JTA in Tomcat, make sure to check this), and get a reference to the EM using the following
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnitName");
em = emf.createEntityManager();
Finally, add the jpa provider jars to Tomcat, there are examples for that (I've never done it, but I'm sure some Tomcat expert could lend a hand)
The other option could also work, that is consume your webservice using a client deployed on another server
Good luck!
If your application uses advanced Java EE functionality (JTA, CDI), it won't work on Tomcat. If it uses JSTL, you will have to add a JSTL library to the tomcat installation. Read the docs, it should be stated somewhere, but I would not simply deploy the war to a tomcat.
If you just want to develop a relay webapp that consumes a webservice and displays the results, it will run on tomcat independantly of where the webservice runs.

Tomcat and OSGi

I was wondering if it is possible to embed an OSGi container like Karaf inside a Tomcat instance. According to this SO question and a few others, it seems like its possible, but I can't seem to find any solid details on how to do this or what pitfalls/caveats to watch out for.
So:
Is it possible to embed an OSGi container inside Tomcat, so that I can hot-deploy OSGi bundles at any point to this container without having to restart Tomcat?
If the answer to #1 above is "yes", then what system services/ports does embedding OSGi inside Tomcat expose? I ask because I would like to deploy OSGi in a Tomcat server hosted on a Java PaaS where I don't have admin rights. And I'm wondering if - when I try to deploy the embedded OSGi container to Tomcat, that it will try to start listening on ports, or perhaps start doing something to the local file system. If this is the case I will likely receive security/permission errors.
Thanks in advance!
Is it possible to embed an OSGi container inside Tomcat?
Yes. In fact if you download Karaf and look in <KARAF_HOME>/demos/web you'll find a demo project that does exactly that.
what system services/ports does embedding OSGi inside Tomcat expose?
That would depend on what you install in it and how you configure it. Here is somewhat old (but IMHO stil relevant) discussion about default ports and how to configure them.
We deploy our application in the same fashion. We have deployed karaf in tomcat & on weblogic using a servelt bridge. The reason for using the container was to get past environment constraints where some customers are a "oracle" or an "ibm" shop and want all deployments done on these servers.
Since you will be using a servlet bridge it does not need to open a new port to list to http traffic. You may have issues with the karaf console if that port is blocked. Also I recall having issues with running cxf due to an embedded jetty instance it starts on another port.
Other than the ports you will need a karaf home directory with write access.

Tomcat or JBoss server for a Java EE application?

This might be duplicate of this and this question.
I have a java application in which i am using JAX-WS (WEB SERVICES). and many other features in it like apache-commons utils.
My application is swing application, that connects to my server code
and uploads and downloads, and sync files with the server. my
database is mysql.
I have compiled my server code and created a .WAR file. i deployed
that .WAR (containing all the required libraries for my server
side application) file on a JBoss server and its working fine.
Now my Management says me to why use JBoss instead of Tomcat, which
easy to manage and setup on a linux server. actualy they have a
linux server which supports tomcat, but not JBoss. so they said to
me to deploy on Tomcat on Linux Server. I installed and configured
tomcat on my windows and deployed a .WAR file. and also created
virtual directory for my resources its working fine[but not tested
completely]
My Application does not have EJB so I think that Tomcat is good enough.
Please suggest me is it fine to use Tomcat instead of JBoss server,
I am worried if it may get problems in the future, or it might not
support features of my application in future or after complete testing.
JBoss uses Tomcat under the hood to host war applications anyway.
Using Tomcat for you war application instead of JBoss should be perfectly fine.
Any commons-jars that you are using can be downloaded from http://commons.apache.org/. Put them in your lib directory and you should be good to go. Most apache commons libraries should allready be included with Tomcat though, you'll have to check.
PS:
JAX-WS is standard in the latest JDK6 updates. If your using a earlier version of the JDK then just download the JAX-WS jars. The standard reference implementation from Oracle/Sun can be downloaded from here http://jax-ws.java.net/
Do you have EJB? JBoss comes with its implementation of the EJB JSR specification.
For Tomcat, you require plugins like openEJB.
Many application servers use Tomcat internally as its Servlet container. But for compliance to JavaEE standards, most would choose a JBoss application server over Tomcat anyday.
Note that JBoss 7 Application Server Web Profile is certified.
Tomcat functions well as a plain vanilla Servlet container. Many enterprises when using Tomcat, couple it with Proxy and HTTPD servers for security and load balancing reasons.

How can I embed Weblogic server in java?

I am searching any way for embedding Weblogic server in Java , I know its possible because we have maven plugins for Weblogic which embeds Weblogic in maven, But googling for it did'nt gave me useful output, Does anybody know how can we embed wemlogic in java program ?
WebLogic doesn't provide an embedded API so, even if it's a pure Java Server and if you can thus call weblogic.Server from Java code, you will have to handle everything yourself (starting the container, waiting until it's started, deploying things, waiting until they are deployed, etc). In other words, this will require some work.
Maybe have a look at the sources of Cargo, although Cargo isn't really starting an embedded Weblogic (i.e. running weblogic.Server in the same JVM). This will give you an idea of what has to be done. Or, depending on your needs, use Cargo Java API.
But if you need a full Java EE server and if this is an option, I would use GlassFish v3 in embedded mode instead of WebLogic, it will be much simpler. Check the following links and see yourself:
Embedding Glassfish V3 in Unit Test - Two Jars, Three Lines Of Code And Five Seconds Start With Deployment
Embedding EJB 3.1 Container Into Your Unit Tests - Boot Time: 5 Seconds
Using the EJBContainer API with or without Maven (but with GlassFish v3)
TOTD #128: EJBContainer.createEJBContainer: Embedded EJB using GlassFish v3
Do you need WLS specifically, of any servlet container would do? If the latter is OK, then use Jetty.
WLS is not designed to be embeddable. But you can do it. After all, WLS is just a class named weblogic.Server. Setup classpath correctly, setup PATH and other environment variables (see setDomainEnv.sh and startWeblogic.sh), start that class from Java, and you have an "embedded" WLS.
There is probably a way, but I don't know it. My experience from writing maven plugins tells me that the most likely way that the plugin works is that it starts up a new command line process just like you would normally start up the server. So in a sense, not really embedded it.
The best way to see is to track down the source of the plugin and see how they did it.
WebLogic doesn't support embedded mode like Glassfish but you can have control over your Weblogic using "WebLogic Maven Plugin", this provide several maven goals for managing and working with Weblogic instance.
See this link for further information.

Categories