Commit 0f281bc9 by Dave Syer

Initialize log4j to prevent blitz4j from freaking out

parent 4e2148d6
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package org.springframework.cloud.netflix.eureka.server; package org.springframework.cloud.netflix.eureka.server;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
...@@ -30,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -30,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.logging.log4j.Log4JLoggingSystem;
import org.springframework.cloud.netflix.eureka.DataCenterAwareMarshallingStrategy; import org.springframework.cloud.netflix.eureka.DataCenterAwareMarshallingStrategy;
import org.springframework.cloud.netflix.eureka.DiscoveryManagerInitializer; import org.springframework.cloud.netflix.eureka.DiscoveryManagerInitializer;
import org.springframework.cloud.netflix.eureka.EurekaServerConfigBean; import org.springframework.cloud.netflix.eureka.EurekaServerConfigBean;
...@@ -44,6 +46,7 @@ import org.springframework.context.SmartLifecycle; ...@@ -44,6 +46,7 @@ import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
...@@ -84,27 +87,40 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware ...@@ -84,27 +87,40 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware
this.servletContext = servletContext; this.servletContext = servletContext;
} }
@Bean @Bean
@ConditionalOnMissingBean(DiscoveryManagerInitializer.class) @ConditionalOnMissingBean(DiscoveryManagerInitializer.class)
public DiscoveryManagerInitializer discoveryManagerIntitializer() { public DiscoveryManagerInitializer discoveryManagerIntitializer() {
return new DiscoveryManagerInitializer(); return new DiscoveryManagerInitializer();
} }
@Override @Override
public void start() { public void start() {
discoveryManagerIntitializer().init(); discoveryManagerIntitializer().init();
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
new EurekaBootStrap() { new EurekaBootStrap() {
@Override @Override
protected void initEurekaEnvironment() { protected void initEurekaEnvironment() {
try {
if (System.getProperty("log4j.configuration") == null) {
System.setProperty("log4j.configuration",
new ClassPathResource("log4j.properties",
Log4JLoggingSystem.class).getURL()
.toString());
}
}
catch (IOException e) {
// ignore
}
LoggingConfiguration.getInstance().configure(); LoggingConfiguration.getInstance().configure();
EurekaServerConfigurationManager.getInstance() EurekaServerConfigurationManager.getInstance()
.setConfiguration(eurekaServerConfig); .setConfiguration(eurekaServerConfig);
XmlXStream.getInstance().setMarshallingStrategy(new DataCenterAwareMarshallingStrategy()); XmlXStream.getInstance().setMarshallingStrategy(
JsonXStream.getInstance().setMarshallingStrategy(new DataCenterAwareMarshallingStrategy()); new DataCenterAwareMarshallingStrategy());
JsonXStream.getInstance().setMarshallingStrategy(
new DataCenterAwareMarshallingStrategy());
// PeerAwareInstanceRegistry.getInstance(); // PeerAwareInstanceRegistry.getInstance();
applicationContext applicationContext
.publishEvent(new EurekaRegistryAvailableEvent( .publishEvent(new EurekaRegistryAvailableEvent(
...@@ -229,12 +245,13 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware ...@@ -229,12 +245,13 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware
} }
/** /**
* Additional aspect for intercepting method invocations on PeerAwareInstanceRegistry. * Additional aspect for intercepting method invocations on
* If {@link PeerAwareInstanceRegistry#openForTraffic(int)} is called with a zero * PeerAwareInstanceRegistry. If
* {@link PeerAwareInstanceRegistry#openForTraffic(int)} is called with a zero
* argument, it means that leases are not automatically cancelled if the instance * argument, it means that leases are not automatically cancelled if the instance
* hasn't sent any renewals recently. This happens for a standalone server. It seems * hasn't sent any renewals recently. This happens for a standalone server. It
* like a bad default, so we set it to the smallest non-zero value we can, so that any * seems like a bad default, so we set it to the smallest non-zero value we can,
* instances that subsequently register can bump up the threshold. * so that any instances that subsequently register can bump up the threshold.
* *
* @author Dave Syer * @author Dave Syer
* *
...@@ -246,7 +263,7 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware ...@@ -246,7 +263,7 @@ public class EurekaServerInitializerConfiguration implements ServletContextAware
if ("openForTraffic".equals(invocation.getMethod().getName())) { if ("openForTraffic".equals(invocation.getMethod().getName())) {
int count = (int) invocation.getArguments()[0]; int count = (int) invocation.getArguments()[0];
ReflectionUtils.invokeMethod(invocation.getMethod(), ReflectionUtils.invokeMethod(invocation.getMethod(),
invocation.getThis(), count==0 ? 1 : count); invocation.getThis(), count == 0 ? 1 : count);
return null; return null;
} }
return invocation.proceed(); return invocation.proceed();
......
...@@ -50,8 +50,8 @@ public class Log4JLoggingSystemTests { ...@@ -50,8 +50,8 @@ public class Log4JLoggingSystemTests {
private Logger logger; private Logger logger;
@Before @Before
public void setup() { public void setup() throws IOException {
System.setProperty("log4j.configuration", getPackagedConfigFile("log4j.properties")); System.setProperty("log4j.configuration", new ClassPathResource("log4j.properties", Log4JLoggingSystem.class).getURL().toString());
logger = Logger.getLogger(getClass()); logger = Logger.getLogger(getClass());
LoggingConfiguration.getInstance().configure(); LoggingConfiguration.getInstance().configure();
} }
...@@ -68,13 +68,4 @@ public class Log4JLoggingSystemTests { ...@@ -68,13 +68,4 @@ public class Log4JLoggingSystemTests {
equalTo(1)); equalTo(1));
} }
private final String getPackagedConfigFile(String fileName) {
try {
return new ClassPathResource(fileName, Log4JLoggingSystem.class).getURL().toString();
}
catch (IOException e) {
throw new IllegalStateException("Cannot create URL", e);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment