How to configure JMX with Spring Boot - spring-boot

I have created a Spring Integration application with Spring Boot. I would like to know how to configure JMX with Spring Boot. I believe by default JMX is configured when using Spring Boot Actuator.
Do I need to configure anything else to be able to export MBeans for Spring Integration?
Most of the example I see have the following line in the applicationContext.xml
<context:mbean-export/>
<context:mbean-server/>
My Application.java class looks like this.
package com.jbhunt.app.consumerappointmentintegration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
#Configuration
#ComponentScan
#EnableAutoConfiguration
#ImportResource("classpath:META-INF/spring/integration/spring-integration-context.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Adding this line to the configuration doesn't seem to export the Spring Integration mbeans
#EnableIntegrationMBeanExport(server = "mbeanServer", defaultDomain="my.company.domain")
I'm referencing this video https://www.youtube.com/watch?v=TetfR7ULnA8

As you understand the Spring Integration JMX is enabled by default, if you just have spring-integration-jmx in the classpath. And, of course, if spring.jmx.enabled = true (default).
You can't overrride that just declaring one more #EnableIntegrationMBeanExport, because it is based on #Import and you just can't override import classes because of (from ConfigurationClassParser):
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
If imported classes are already there, they aren't overridable.
You have several choices to achieve your requirements:
Disable default Spring Boot JMX - just addind to the application.properties spring.jmx.enabled = false and continue to use #EnableIntegrationMBeanExport
Configure IntegrationMBeanExporter #Bean manually.
Just configure your my.company.domain domain in the application.properties:
spring.jmx.default_domain = my.company.domain

It is quite late to add this; but in addition to the endpoints.jmx.domain I found it useful to change the spring.jmx.default-domain to someting unique per application
This is with multiple instances of Spring Boot 1.4.1 apps running in Tomcat 7

Related

How do I use the OpenTelemetry component in Apache Camel?

Using the Spring Boot example from Camel, I'm trying to set up OpenTelemetry to instrument Camel.
Based on the docs, I chose the Spring Boot Auto-configuration option and added the dependency to my pom.xml.
I've also annotated the main class with #CamelOpenTelemetry:
package sample.camel;
import org.apache.camel.opentelemetry.starter.CamelOpenTelemetry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//CHECKSTYLE:OFF
/**
* A sample Spring Boot application that starts the Camel routes.
*/
#CamelOpenTelemetry
#SpringBootApplication
public class MyCamelApplication {
...
I started the application with OTEL_TRACES_EXPORTER=logging mvn spring-boot:run.
Is this correct? I'm not seeing any console output unlike this simple OpenTelemetry example.
I'm not sure if Camel 3.x is supported because this page says only 2.20+ is supported but Camel's docs says Since Camel 3.5 at the top.

Spring Boot's embedded Tomcat ignores access log configuration

I've enabled the access log as follows:
server.tomcat.basedir=/var/log/my-server/tomcat
server.tomcat.accesslog.directory=.
server.tomcat.accesslog.enabled=true
This usally works in my Spring Boot projects, but not in my current project (Spring Boot 2.6.6), where I'm not using #SpringBootApplication due to some limitations.
I'm currently using the following annotations for my main class:
#SpringBootConfiguration
#ComponentScan
#ServletComponentScan(basePackageClasses = { /* ... */})
#ImportAutoConfiguration({WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
ServletWebServerFactoryAutoConfiguration.class})
public class ...
Is there any autoconfiguration I need to import explicitly to configure the embedded tomcat? (server.address as well as server.port gets already respected).
You need to import EmbeddedWebServerFactoryCustomizerAutoConfiguration as well.

Auto configuration at spring

In Spring framework, If I use Java and annotation based configuration.
Should I need to add manual configuration for spring-security or spring-Aop?
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import
org.springframework.web.servlet.config.annotation
.WebMvcConfigurerAdapter;
#ComponentScan("com.ViewsController")
#Configuration
#EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter{
}
No need of extra configuration to use Security and Aop in annotation based spring project, just using appropriate annotations on correct location is enough.
like use #EnableWebSecurity on a class which extend WebSecurityConfigurerAdapter to enable spring security.
In order to authenticate user and role you have to override the super class method and implement your custom authentication .
Use the following link for spring security more implementing spring security.
And for AOP use #Aspect and #EnableAspectJAutoProxy annotations on class, which contains apspectj advice's.
In Annotation based java project, using annotation is enough, follow the tutorials on spring-aop, spring-security

Loading application.properties file in a non SpringBoot application

I've a set of microservices built with spring boot and using feign as client. All is working perfectly, but I have a problem with a non SpringBoot application.
In this case I would like to use configuration properties file (application.properties) to configure different client (like Ribbon).
In my configuration bean I've included #ImportAutoConfiguration for all the components, but configuration is not loaded from properties file.
Is there a way to perform this?
Thanks!
import org.springframework.context.annotation.PropertySource;
#RequestMapping("/url")
#Controller
#PropertySource("classpath:config.properties")
public class StudentController{
#Autowired
private Environment env;
//get properties using keys
//env.getProperty("key");
}
and put config.properties file inside src/main/resources

spring tool suite can not deploy example webservice on tomcat

I have some experience with EJB and JBoss and know the basics about Webservices, but I'm new to Spring.
So I tried to deploy the example Spring WS project gs-rest-service-complete without any changes. It is running on Spring Boot, but I cannot deploy and access it on an external Tomcat Server.
This is what I did: I installed Spring Tool Suite Version: 3.6.4.RELEASE and
Apache Tomcat 8.0.24 and defined Tomcat as a new Server in Spring Tool Suite.
It seems to work because I can deploy (and access) the example Spring MVC project and I can deploy another (not Spring Example) Webservice on Tomcat.
However I cannot deploy the gs-rest-service-complete project. I changed packaging in pom.xml to 'war', but it didn't help. Any hints what I could do?
Thanks, caduta
Finally I found the answer at the bottom of this site:
https://spring.io/blog/2014/03/07/deploying-spring-boot-applications
I had to do three steps to get it running:
change packaging to war in pom.xml.
comment out the declaration of the spring-boot-maven-plugin in pom.xml.
change the Application class to inherit from SpringBootServletInitializer and override method configure. This is necessary to register the application in tomcat.
Now the Application class looks like this:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
private static Class<Application> applicationClass = Application.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
}

Resources