Setting Runtime Property in Web Application - java

I am planning to use JGroups in a web application.
JGroups by default uses IPv6 for multicasting of messages. JGroups can only be configured to use IPv4 by setting a property like the following (see docs)
-Djava.net.preferIPv4Stack=true
This does not work if set in code. What are my options when running a war file in an application server for setting this property, specifically tomcat and glassfish?

It seems to me the following route might work. First, add a ServletContextListener to your web app and register it in web.xml before JGroups is loaded.
Now, inside the contextInitialized method, use System.setProperty("java.net.preferIPv4Stack", "true").
Disclaimer: not tested.

Unless you have a specific reason to put this in code, it can easily be configured for Tomcat by setting the environment variable CATALINA_OPTS (to -Djava.net.preferIPv4Stack=true). I think the proper place to do this is to create ${CATALINA_HOME}/bin/setenv.sh and place the setting in there.

Related

Is it possible that we can configure and run web server automatically?

Generally, we downloaded the TOMCAT to the computer and configure the context where it stores the project path then run the TOMCAT. So we can access our web project.
Is there a possibility that we can start the TOMCAT by just using java code.
like
Tomcat tomcat = new Tomcat(configuration);
tomcat.run();
Can not understand what are you trying to achieve? But, Spring Boot comes with embedded tomcat and your application is deployed automatically on tomcat (with default configurations). If you want to change configurations for the tomcat you can specify those configurations in application.properties.
Like, e-g if you want to change port of your server you can specify following property in application.properties.
server.port = 8085
There are other server related properties for which you can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html.
You can also implement WebServerFactoryCustomizer
in order to configure tomcat with java. For this purpose you can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html

WebSphere liberty class loading PARENT LAST as default

I have been investigating using WebSphere's Liberty profile as a light weight alternative to having a fully fledged WebSphere instance deployed on my local machine (late to the party, I know).
1 thing that I can not figure out:
How do I set Parent Last class loading to be default?
I want to deploy any application and have it automatically be parent last.
I am aware that I can deploy an EAR with the deployment.xml to trigger parent last class, or run a Jython script. However, I would rather have it be the default behavior.
Any ideas anyone?
WebSphere Version: Liberty 8.5.5.8
If you're trying to set parent-last classloading in Liberty because you had to do so in WebSphere Application Server traditional to resolve library conflicts, you should first try the application on Liberty without changing it's configuration because Liberty was designed to avoid those types of conflict. If, after that, you're still inclined to change the configuration, while you can't set it as the default, you can do something like this in your server.xml:
<enterpriseApplication location="myApp.ear" name="MyApp">
<classloader delegation="parentLast"/>
</enterpriseApplication>

Spring boot define preferred port

I write a spring boot application (which is configured throw a Spring Cloud Config Server).
This application has a small UI for information only. However, it should be possible to start this application multiple times so you can not set a fix port for this application (in the Config Server).
For this it is possible to set a random port (server.port=0) and you can start as many instances of this application as you want. A little issue is that if the user wants to access the UI he has to find out a “random port”.
So my question is: Is it somehow possible to define a preferred Port (using Config Server) and only if this port is not available take a random port?
You can just configure it via command line system property java -Dserver.port=$PORT -jar app.jar. Example is in this section of Spring Boot docs.
I bet also setting up environment variable SERVER_PORT may work.
I think also Spring Boot parameter should work: java -jar app.jar --server.port=$PORT

How to know if my application is running over a JBoss server?

I am developing a GWT application.
And I would like to know if I am running over a Jboss instance or over a Jetty instance.
This is because if I am running over Jetty it means that I am running the dev mode and I need to redirect to MyModule.html?codeserver... but if I am running over Jboss I need to redirect to MyModule.html
But I can't figure how can I know if I am running over Jetty or over Jboss.
You should not try to find out the webserver on which your application is running, in your application code. Instead you can have a discriminating parameter set in the context of your application with different values for different servers.
E.g.
For JBoss, the server.xml can contain a parameter in the context definition as follows:
<Context ...>
...
<Parameter name="applicationStage" value="prod" />
...
</Context>
and for Jetty, the same context parameter would go into its jetty.xml but the value as "devo".
If you just want to know wether you are running production or development mode, then try
GWT.isProdMode();
Only works client-side of course.
There's not really any simple way to determine what application server you're running on (to my knowledge). You could check some system properties to determine this.
JBoss:
jboss.server.name
Jetty:
jetty.home
Although I do not encourage having logic determined by your environment as Vikdor mentions in his answer. His suggestion would be a better approach.
In a JSP,
Server is <%= application.getServerInfo()%>
or use
Server is ${pageContext.servletContext.serverInfo}
in a Servlet,
String server = getServletContext().getServerInfo();

felix exthttpservice set session path for cookies

I have a webapplication that I am running in Felix osgi container. I am using jetty as the implementation for the extHttpService. Currently it is writing the cookies to the '/' root path. I would like to change this as it is causing conflicts with other web applications. Looking at jetty documentation it appears I need to set the following property.
org.mortbay.jetty.servlet.SessionPath
However, I am unable to find a way to set this using the ExtHttpService via osgi. I have tried creating a jetty.xml file, adding this to the config.properties, and setting it as a property in the call to register my servlet.
Does anyone know how to set this?
thanks,
I actually ended up patching the source for my current implementation, but on the mailing lists here, a patch has been submitted that should allow this to be configurable.

Categories