Setting Spring #Profile from JNDI for SpringBoot - spring

I'm deploying my SpringBoot Application to a Tomcat 8.5 container.
Similar as described here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html I modified my SpringBootApplication, so it's deployable as war.
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
Similiar to this question Activating Spring #Profile Using JNDI I wan't the application to select the active Profile using an JNDI entry.
I added an EnvironmentApplicationContextInitializer identical to the one shown in the accepted answer.
However: I do not use any web.xml. So the profile is not being picked up and used.
How do I make the SpringBootApplication using the EnvironmentApplicationContextInitializer?

Thanks to M Deinum's comment I found a solution:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
// this would be used if run via java -jar service.war
// SpringApplication app = new SpringApplication(Application.class);
// CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
// app.addInitializers(initializer);
// app.run(args);
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// this will be used within an app container
CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
return builder.initializers(initializer).sources(Application.class);
}
}
Note the builder.initializers(initializer) part.

Related

In Spring-based web application, where is my console/log output going to?

I'm writing a simple Spring-based web application and deploying it to Websphere Liberty 8.5.5.9; I've gotten past my deployment problems and the application seems to start (according to the Liberty console.log). However, I'm not seeing any console or log output. My application main class, which contains print statements in the main method, is:
#Configuration
public class UserSettingApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
ServletContext servletContext;
private static final LoggerUtils logger = new LoggerUtils( UserSettingApplication.class );
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(UserSettingApplication.class);
return builder;
}
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(RequestContextListener.class);
this.servletContext=servletContext;
super.onStartup(servletContext);
}
public static void main(String[] args) throws Exception {
System.out.println( "Entering UserSettingApplication.main" );
SpringApplicationBuilder applicationBuilder = new UserSettingApplication().configure(new SpringApplicationBuilder());
applicationBuilder.run(args);
System.out.println( "Entering UserSettingApplication.main" );
}
#Override
protected WebApplicationContext run(SpringApplication application) {
WebApplicationContext webApplicationContext = super.run(application);
Environment env = webApplicationContext.getEnvironment();
String sessionName = env.getProperty("server.session.cookie.name", "xplore-session-id");
servletContext.getSessionCookieConfig().setName(sessionName);
return webApplicationContext;
}
#Bean
protected RequestContextListener requestContextListener() {
return new RequestContextListener();
}
#Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
logger.info("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
logger.info(beanName);
}
};
}
}
Shouldn't I be seeing the print statements in the main method in the WASLiberty console.log?
Shouldn't I be seeing the print statements in the main method in the WASLiberty console.log?
You will not see any printouts from the main method as it is not executed in the Liberty. The main method is used for standalone apps started from the command line not run from the app server. Put your messages in the configure method like below and you will see it.
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
System.out.println("########################################Starting app");
System.out.println() will NOT write into files such as console.log.
Instead it will write into console such as commandline window or eclipse console.
You need to use loggers such as log4j or Java.util.logging for writing into files.
You can find more info here what-is-the-difference-between-java-logger-and-system-out-println

schedule task not working after deploying spring boot project in jboss, every thing works well except schedule tasks

schedule task not working after deploying spring boot project in jboss, every thing works well except schedule tasks
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception
SpringApplication.run(Application.class);
}
}
//this is schedule task class in diffrent package
#Component
public class ScheduledTasks {
scheduled(fixedDelay = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateTest.format(new Date()));
}

Spring boot deployment issue on jboss

After deploying spring boot project on jboss ,every things work great except my schdule task.i have annotated my main class with #enablescheduling and #component on my schedule method class and #scheduled on my method.but after running successfuly without error on jboss ,my schedule task does not fire.i really need a solution.
Tnx every body
Below is my config class and my schedule classes:
#SpringBootApplicatio
#Enablescheduling
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
#component
Public class test{
#scheduled(fixedDelay = 1000)
Public void print(){
//do sth
}
}
#SpringBootApplicatio
#Enablescheduling
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
#component
Public class test{
#scheduled(fixeddelay.....)
Public void print(){
//do sth
}
}
Ok, we've found an issue. Setting initialDelay to 10000 ms resolved the problem in our case. Don't know an explanation but still: it works.

Hystrix turbine is not working

I am configuring hystrix turbine dashbord using ConfigPropertyBasedDiscovery . When I hit normal stream URL, it works fine
http://localhost:8080/turbine.stream?cluster=EXAMPLE
But when I try to load this cluster stream in dashbord it show below error "unnable to connect command matrix" on browser and on console "EventSource's response has a MIME type ("text/plain") that is not "text/event-stream". Aborting the connection.". Please have look on screen shot.
Below is my config.properties
turbine.aggregator.clusterConfig=EXAMPLE,EXAMPLE1
turbine.instanceUrlSuffix=:9080/hystrix.stream
turbine.ConfigPropertyBasedDiscovery.EXAMPLE.instances=localhost
turbine.ConfigPropertyBasedDiscovery.EXAMPLE1.instances=139.126.244.170
InstanceDiscovery.impl=com.netflix.turbine.discovery.ConfigPropertyBasedDiscovery
My Application.java
#EnableHystrixDashboard
public class Application extends SpringBootApplication {
private static Class<Application> applicationClass = Application.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
TurbineInit.init();
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
super.configure(application);
return application.sources(applicationClass);
}
}
And servlet registration for "/turbine.stream"
#Configuration
public class ServletRegistrationConfig {
#Bean
public ServletRegistrationBean registerTurbineBean(){
return new ServletRegistrationBean(new TurbineStreamServlet(), "/turbine.stream");
}
}
Please assist me in this. And let me know if I missed out in configuration.

Spring Boot Programmatically setting profiles

How to set active profile in spring boot Application. This application will be deployed in stand alone Tomcat.
I have 2 property files application-{profile}.properties.
My Application class
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
if I run the app with embedded tomcat the dev profile is set as active and it works fine. But when I deploy in stand alone tomcat. It does not work.
I tried to set active profile in configure method. but i get null pointer exception, when i get the environment from the context.
Any help on how to set the active profile.
You can set additional profiles on start up:
SpringApplication springApp = new SpringApplication(Main.class);
springApp.setAdditionalProfiles("profile1", "profile2");
springApp.run(args);
I also had the same problem and after struggling for half a day I ended up with this:
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
SpringApplication.run(MyApplication.class, args);
}
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
super.onStartup(servletContext);
}
}
Another way of doing it in Spring Boot 2 is by using SpringApplicationBuilder:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
#SpringBootApplication
public class MySpringProgram {
public static void main(String[] args) {
new SpringApplicationBuilder(MySpringProgram.class)
.profiles("profile1", "profile2")
.run(args);
}
}
Instead of activating the profile dynamically, you can put the profiles as vm-arguments in the catalina.sh
CATALINA_OPTS="-Dspring.profiles.active=dev"
System.setProperty("spring.profiles.active", "dev");
SpringApplication app =new SpringApplication(Application.class);
Properties props = new Properties();
props.put("AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAM", "dev");
app.setDefaultProperties(props);
app.run(args);
I think this code is better solution, because it doesn't set system property

Resources