Configuration jetty server logging - spring

We would like to make jetty use log4j configuration for logging. I am using embedded jetty within spring based on this tutorial. I am followed this documentation to include all required slf4j and log4j dependencies in classpath.
Basically I added jetty-logging.properties to classpath with following configuration.
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4Log
org.eclipse.jetty.LEVEL=INFO
I also have log4j.properties in classpath
I have following maven dependencies to include required jars.
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.log4j.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.maven.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.9.v20130131</version>
</dependency>
But when I start up the jetty server via spring, the log output is always logged to StdErr and it's not logging to log file I configured in log4j.properties. Am I missing anything? Is there any debugging I can turn on to troubleshoot ..? How can I get this to work?

Though the answer to this question did help me find the answer, it took me a little bit more digging.
I was using Spring MVC with log4j. The following
public class LaunchServer {
private static final int SERVER_PORT = 8008;
public static void main(String[] args) throws Exception {
//Make sure you set the log before you start the server
Log.setLog(new Slf4jLog());
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(SERVER_PORT);
server.addConnector(connector);
WebAppContext wac = new WebAppContext();
wac.setWar("war");
wac.setContextPath("/");
wac.setMaxFormContentSize(5242880);
wac.setMaxFormKeys(100000);
wac.setClassLoader(Thread.currentThread().getContextClassLoader());
server.setHandler(wac);
server.setStopAtShutdown(true);
server.start();
}
}
In my log4j2.xml file under the <Loggers> I put
<Logger name="org.eclipse.jetty" level="debug" />
It worked well for me and avoided everything being logged to StdErr

I got this answered via jetty mailing list.

Related

camel springboot and hawtio integration

I am working on integrating the camel springboot component with hawtio. I can't see the camel tab in the hawtio UI. I am using below dependencies of springboot camel and hawtio
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>io.hawt</groupId>
<artifactId>hawtio-springboot</artifactId>
<version>2.10.1</version>
</dependency>
This the sample route i configured, route is working as expected
#Component
public class SampleRoute extends org.apache.camel.builder.RouteBuilder{
#Override
public void configure() throws Exception {
from("kafka:test123?brokers=localhost:9092&allowManualCommit=true&autoCommitEnable=false&breakOnFirstError=true&maxPollRecords=1&synchronous=true")
.process(exchange -> {
exchange.getIn().setBody("test1234");
}).to("stream:out");
}
}
After reading many articles , i added below set of properties and it seems to have any effect.
management.endpoints.web.exposure.include=hawtio,jolokia
management.endpoints.jolokia.sensitive=false
endpoints.jolokia.sensitive = false
hawtio.authenticationEnabled=false
spring.jmx.enabled=true
camel.springboot.endpoint-runtime-statistics-enabled=true
management.endpoint.camelroutes.enabled=true
management.info.camel.enabled=true
management.endpoint.camelroutes.enabled=true
management.endpoint.camelroutes.read-only=true
camel.springboot.jmx-enabled=true
One of the solution worked for me is by downgrading to camel 2.X , but no luck with camel 3.X
I got it working, following the link as suggested. I had to remove the conflicting dependencies and adding below dependency worked.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<version>3.2.0</version>
</dependency>

ElasticSearchHighLevelClient springboot Autoconfiguration not working

I am trying to autoconfigure elasticsearch high-level rest client using this documentation
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-elasticsearch
I added the following properties to my application.properties:
spring.elasticsearch.rest.uris=https://hostname.com
spring.elasticsearch.rest.read-timeout=10s
spring.elasticsearch.rest.username= user
spring.elasticsearch.rest.password= password
I added these dependencies in my pom.xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
and a property
<elasticsearch.version>7.2.1</elasticsearch.version>
In my application I did this
#Configuration
#EnableAutoConfiguration
#SpringBootApplication
#ComponentScan
public class MainClass {
public static void main(String[] args) {
SpringApplication.run(MainClass.class, args);
}
}
But when I try to use the HighLevelClient it gives me NullPointerException
#Autowired
private RestHighLevelClient client;
Am I missing something?
You should add the stacktrace for the NullPointerException.
You did not write which versions you use (Spring Boot, Spring Data Elasticsearch). But the current Spring Boot 2.2.2 targets Spring Data Elasticsearch 3.2.x which uses Elasticsearch 6.8.5. So this won't work with an Elasticsearch 7 cluster. Support for Elasticsearch 7 is coming with the next version (Spring Data Elasticsearch 4.0)
I hope you are using spring-boot-autoconfigure dependency because RestHighLevelClient bean is initialized by this library based on the presence of RestHighLevelClient in the classpath. Check the dependency tree for the presence of spring-boot-autoconfigure.

How to activate /turbine.stream endpoint in a standalone Turbine application

I am trying to create a standalone application to collect Hystrix stream from other applications. But it does not expose the /turbine.stream endpoint by default. I am sure what is missing in my project.
Spring Boot: 2.0.4.RELEASE, Spring Cloud: Finchley.SR1
The application class:
#SpringBootApplication
#EnableDiscoveryClient
#EnableTurbine
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The content of applicaiton.yml:
server:
port: 8383
spring:
application:
name: hystrix-turbine
management:
endpoints:
web.exposure.include: '*'
applications: hystrix
turbine:
aggregator:
clusterConfig: ${applications}
appConfig: ${applications}
# instanceUrlSuffix.default: actuator/hystrix.stream
And the maven dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I created a sample project for this.
I'd suggest you check the configuration steps below:
1) Your stream URL in the Hystrix Dashboard should be:
http://localhost:{turbine app port}/turbine.stream?cluster={configured cluster in properties file}
The url should be pointing to the port of the application that has #EnableTurbine annotation in your main class.
2) Check if you are getting a response for:
http://localhost:{client app port}/actuator/hystrix.stream
(use your browser for this) (this should be coming from the application you have enabled hystrix on using #EnableCircuitBreaker)
If you're getting pings, then atleast your hystrix stream is accessible. If not, Check if you have: org.springframework.boot:spring-boot-starter-actuator in your client side dependencies and
make sure you have the below property set in application.properties file of the application that has #EnableCircuitBreaker in the main class:
management.endpoints.web.exposure.include= hystrix.stream, info, health
Check the URL again.
3) Once your get a reponse from hystrix.stream, you can now configure your cluster on the turbine app properties file:
turbine:
appConfig: {serviceId in lower case}
aggregator:
clusterConfig: {above serviceId in upper case}
after running the app, check if you've configured the cluster correctly:
http://localhost:{turbine app port}/clusters
you should'nt be getting a "[]" on your browser if all's well.
Once you see a response on the clusters endpoint, you will now be able to see the details on the dashboard when you point it to the turbine app

using embedded amqp swith spring boot and camel

I want to integration test a custom camel component and therefor need an embedded/in memory messanging I can easily use to test from/to endpoints.
I am hoping that I can achieve this via spring-boot-amqp-starter.
I used this example for a start, which has the dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
and the config:
spring:
activemq:
broker-url: vm://embedded?broker.persistent=false,useShutdownHook=false
// ...
This is working, when I use regular Listener annotations in spring, I have a sender using the template and a consumer logging the messages.
Now I go one step further and use camel, but it does not recognize the vm:embedded broker but tries to connect to tcp://localhost, which is not running.
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from("activemq:foo").to("log:sample");
from("timer:bar").setBody(constant("Hello from Camel")).to("activemq:foo");
}
};
How can I configure activemq-camel to use the embedded broker?
Update:
I use dependency management imports for spring-boot-dependencies (1.5.9)
and camel-spring-boot-dependencies (2.20.1).
This has been fixed in newer versions of activemq-camel when you use that with Spring Boot. Now the activemq-camel component will honor the spring boot configuration of the spring.activemq.* settings.

Spring boot and apache spark - container conflict

I am trying to use spring boot 1.1.5 and apache spark 1.0.2 together in project. Look like apache spark uses Jetty container internally and I have configured spring-boot to use Tomcat container. However application startup fails with some securityException at root cause. If I see full stack trace looks like spring boot trying to initialize "jettyEmbeddedServletContainerFactory" which it shouldn't in first place. It probably picks it up from classpath due to jetty presence via spark. If I exclude jetty from spark and run again I don't see same error again but then SparkContext initialization fails due to not finding jetty. How do i tell spring-boot runtime to look for "TomcatEmbeddedServletContainerFactory" instead of jetty one?
I got "java.lang.SecurityException: class "javax.servlet.http.HttpSessionIdListener"'s signer information does not match signer information of other classes in the same package"
To fix this issue I was need to remove all javax.servlet dependencies.
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.3.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>javax.servlet</artifactId>
</exclusion>
</exclusions>
</dependency>
#Joakim Erdfelt, Thanks.
I was just waiting to see if someone is familiar with this situation and if it's just a small configuration change. As it turns out it is! #Configuration
#EnableAutoConfiguration(exclude={EmbeddedServletContainerFactory.class})
public class MyConfiguration { }
I defined my own "EmbeddedServletContainerFactory" bean as a "org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContaine‌​rFactory" and it started working as I expected.

Resources