camel mbean management objects doesn't appear by default - spring-boot

I use camel 3.3.0 with spring boot 2.3.1.
Considering documentation camel mbeans management objects for camel routes should be appeared by default, but it doesn't happen.
I have tried to add to application.properties all properties with jmx, but it haven't helped too.
application.properties
camel.component.jmx.customizer.enabled=true
camel.springboot.jmx-enabled=true
camel.component.jmx.enabled=true
I expect to see package org.apache.camel in Java Mission Control but I don't. I have old project with came. 2.x.x where everything works correctly.
What do I do wrong?
pom.xml
..............................
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
..............................
<properties>
<java.version>11</java.version>
</properties>
..............................
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jmx-starter</artifactId>
<version>3.3.0</version>
</dependency>
..................................
DemoApplication.java
#SpringBootApplication
public class DemoApplication extends RouteBuilder {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void configure() throws Exception {
from("activemq:queue:test").log("message from test");
}
}

You need to add camel-management JAR as dependency to have Camel JMX enabled.
Its also documented in the upgrade guide: https://camel.apache.org/manual/latest/camel-3x-upgrade-guide-3_1.html#_spring_boot_jmx

Related

Spring boot index page not mapped to / in Tomcat deployment

Yet another issue with deploying Spring Boot WAR to Tomcat... I have read the dozen of similar questions but have not found any fix for my issue.
I have a Spring Boot web app which is working fine when using the embedded tomcat web server (I can reach the index.html page using localhost:8080).
However when I deploy the WAR to Tomcat (the war is called ROOT.war so am I deploying the app at Tomcat's root), localhost:8080 returns 404. I need to call localhost:8080/index.html to get an answer. I just cannot figure out why!
pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>war</packaging>
<properties>
<java.version>11</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
...
</dependencies>
<build>
<finalName>ROOT</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
<executions>
...
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application.java
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I have one #RestController which does not override "/", and that's it.
In the generated WAR, the index.html is located at the top level (so same level as WEB-INF).
I also noticed that when Tomcat starts the web app, it prints:
INFO ServletWebServerApplicationContext ServletWebServerApplicationContext.prepareWebApplicationContext(ServletWebServerApplicationContext.java:292) [main] Root WebApplicationContext: initialization completed in 1674 ms
INFO WelcomePageHandlerMapping WelcomePageHandlerMapping.<init>(WelcomePageHandlerMapping.java:53) [main] Adding welcome page: ServletContext resource [/index.html]
I find the second line strange: it looks like Spring Boot is choosing to default back to a WelcomePageHandlerMapping instead of using the expected spring boot context. No idea where that comes from.
Maybe another indication: it does not print
Initializing Spring embedded WebApplicationContext
while this is printed when I start the app using the embedded Tomcat web server. But maybe it is fine if it is not there.
Tomcat version: 9.0.65
Tomcat config: default config: did not change anything there since installation.
Help!
I could reproduce!
With:
Dockerfile:
FROM tomcat:9.0.69-jre17-temurin-jammy
ARG WAR_FILE=target/ROOT.war
RUN addgroup --system tomcat \
&& adduser --system --ingroup tomcat tomcat \
&& chown -Rfh tomcat:tomcat $CATALINA_HOME
USER tomcat:tomcat
COPY ${WAR_FILE} $CATALINA_HOME/webapps/
CMD ["catalina.sh", "run"]
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- max spring boot version for tomcat 9 (servlet-api): -->
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>traditional</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<!-- latest tomcat9 version, property controls spring dependency management: -->
<tomcat.version>9.0.69</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ROOT</finalName>
<!-- no spring-boot plugin!(?) -->
</build>
</project>
App/Entry:
package com.example.traditional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class TraditionalApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TraditionalApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TraditionalApplication.class);
}
}
(Some /custom controller, (mockMvc) tests)
and a "static index.html" in src/main/webapp (maven war default):
<html>
<body>
<h1>Hello</h1>
Hello World!
</body>
</html>
the (embedded tomcat) test succeeds:
#WebMvcTest
public class WebTest {
#Autowired
MockMvc mockMvc;
#Test
void testRoot() throws Exception {
mockMvc
.perform(
get("/")
).andExpectAll(
status().isOk(),
forwardedUrl("index.html")
);
} // ...
}
but after:
mvn clean install \
&& docker build -t my/tomcat9-app . \
&& docker run -p 8080:8080 my/tomcat9-app,
we get:
404 (tomcat error page) from http://localhost:8080
(http://localhost:8080/index.html, http://localhost:8080/custom work as expected ;(#
Simplest Solution
Move index.html from src/main/webapp to src/main/resources/static ! (stop running container, repeat mvn clean install && docker build ... && docker run);p #

Spring WebFlux and MVC config in the same library

I'm looking for a way to implement a "switch" in my library so that it would be able to work in 2 modes - reactive and blocking.
As of now i have two libraries doing basically the same thing - set of filters and interceptors and a factory providing end user with preconfigured WebClient/RestTemplate, Authentication and Authorization.
I found that SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE) is supposed to force application to run in either a reactive or an mvc mode. However, when i add mvc-dependency i can see in log that Tomcat is being chosen as a container to run the app
Starting service [Tomcat]
Here's my pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
And the Application class
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setWebApplicationType(WebApplicationType.REACTIVE);
app.run(args);
}
}
Am i doing something wrong or the property is not supposed to allow switching between Netty and Tomcat to start with?

Spring boot webflux deployment

I have a project in spring boot reactive with webflux. Someone could tell me how to deploy it on Netty please?
Thanks
The Spring boot starter webflux has an embedded Netty so including this dependency in your project and declaring a class with a main method will launch your reactive web application in Netty.
For example if you use Maven add those dependencies :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
And then in your class containing the main method:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Or if you want a fresh start with everything already configured and ready to code : https://start.spring.io.

spring boot + jboss = page 403

I was trying to connect spring boot with jboss for a week... I read many topics on this forum and many of them are too old. I get error 404 every time when i want run server. Can you please help me to config my app with jboss properly?
UPDATE:
Thank you for help with error 404. I changed package to "war". But now when I run wildfly server Im getting error 403 (forbidden). I was looking how to fix it and still stay in the same place. It is so hard to get wildfly work with spring boot...
I updated files:
https://github.com/kuzyn007/LibraryCRUD
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.seweryn</groupId>
<artifactId>LibraryCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Spring: boot starter parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<!-- Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Starter for using Spring Data JPA with Hibernate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Starter for testing Spring Boot applications with libraries including
JUnit, Hamcrest and Mockito -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- The spring-boot-devtools module can be included in any project to
provide additional development-time features. -->
<!-- Applications that use spring-boot-devtools will automatically restart
whenever files on the classpath change. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- JSP Standard Tag Library -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet.jsp</groupId>
<artifactId>jboss-jsp-api_2.2_spec</artifactId>
<version>1.0.2.Final</version>
</dependency>
<!-- … -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- … -->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<finalName>LibraryCRUD</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
# MVC
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
# JNDI
spring.datasource.jndi-name=java:jboss/datasources/library
# JPA/HIBERNATE
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=pl.seweryn
spring.jpa.database=H2
spring.jpa.show-sql=true
Application.java
package pl.seweryn.init;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
//#Configuration
//#EnableAutoConfiguration
//#ComponentScan
#SpringBootApplication // same as #Configuration #EnableAutoConfiguration #ComponentScan - alternative
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, args);
}
}
Follow this instructions: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
You basically have to package your app as a WAR and implement SpringBootServletInitializer.
I found many mistakes in my project and now its working. I will write here answer for my app.
Physical naming was wrong. It should be: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. Or I could delete this line and it will be provided default from spring boot
I have Application.class in pl.seweryn.init package. I added #ComponentScan(pl.seweryn)
BookDaoImpl was not a spring component. Added line #Component

deploy spring boot websocket sample application to tomcat7

Following tutorial helped me to get a spring boot application up and running real fast: https://spring.io/guides/gs/messaging-stomp-websocket/
But now I'm stuck trying to generate a war file and deploying it on my tomcat7 application server.
I followed the [instructions][1] to create a deployable war file, but this just isn't working. I don't see any errors in the logs, but I don't see my nice sample application either when I browse to http://localhost:8080.
Here are the steps I took to generate a war file:
1) Modify pom.xml by changing the packaging to war and marking the spring-boot-starter-websocket as provided
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
</dependencies>
2) Modify Application.java to override the SpringBootServletInitializer configure method.
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3) Then I run
mvn clean package
and deploy the generated war-file, but I just keep getting the default Tomcat welcome page, when I browse to http://localhost:8080/.
I also tried changing the application contextPath by adding the following application.properties:
server.contextPath=/websocketclient
server.port=8082
But this doesn't solve the problem.
I get it working, I am using Tomcat 8.
Take a look at my code here:
https://github.com/pauldeng/gs-messaging-stomp-websocket
Read and edit the pom file accordingly and run mvn package to create the package specified.
I made a mistake in my pom.xml, the provided scope shouldn't be added to the spring-boot-starter-websocket artefact. But you should add the spring-boot-starter-tomcat artefact with the provided scope.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
</dependencies>
I also had to modify my javascript to contact the websocket, which is now under /gs-messaging-stomp-websocket-0.1.0
var socket = new SockJS('/gs-messaging-stomp-websocket-0.1.0/hello');
The deployed application war needs to be called ROOT.war otherwise you will have to configure server.xml.
Second you should turn on the NIO connector for websockets see server.xml and http://tomcat.apache.org/tomcat-7.0-doc/web-socket-howto.html

Resources