下面列出了怎么用io.netty.util.ResourceLeakDetector.Level的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) throws Exception {
ResourceLeakDetector.setLevel(Level.DISABLED);
// CtClass.debugDump = "d:/debugDump";
// RestClientBenchmark clientBenchmark = new RestClientBenchmark();
// System.out.println(clientBenchmark.createUser());
// clientBenchmark.close();
Options opt = new OptionsBuilder()//
.include(RestClientBenchmark.class.getSimpleName())//
.warmupIterations(5)//
.measurementIterations(5)//
.threads(CONCURRENCY)//
.forks(1)//
.build();
new Runner(opt).run();
}
@Before
public void setUp() throws Exception {
ResourceLeakDetector.setLevel(Level.PARANOID);
// Use MockAltsHandshakerStub for all the tests.
AltsHandshakerOptions handshakerOptions = new AltsHandshakerOptions(null);
MockAltsHandshakerStub clientStub = new MockAltsHandshakerStub();
MockAltsHandshakerStub serverStub = new MockAltsHandshakerStub();
client = new AltsHandshakerClient(clientStub, handshakerOptions);
server = new AltsHandshakerClient(serverStub, handshakerOptions);
}
public static void main(String[] args) throws Exception {
ResourceLeakDetector.setLevel(Level.DISABLED);
// CtClass.debugDump = "d:/debugDump";
try (TurboServer server = new TurboServer("shop", "auth");) {
Map<Class<?>, Object> services = Map.of(UserService.class, new UserServiceServerImpl());
server.registerService(services);
/*
* server.addFirst(new RpcServerFilter() {
*
* @Override public void onSend(Request request, Response response) { try {
* Tracer tracer = request.getTracer();
*
* if (tracer != null) { response.setTracer(tracer); }
*
* } catch (Exception e) { e.printStackTrace(); } }
*
* @Override public boolean onRecive(Request request) { try { Tracer tracer =
* request.getTracer();
*
* if (tracer != null) { RemoteContext.getClientAddress().toString();
* RemoteContext.getServerAddress().toString();
* RemoteContext.getServiceMethodName();
*
* TracerContext.setTracer(tracer); } } catch (Exception e) {
* e.printStackTrace(); }
*
* return true; }
*
* @Override public void onError(Request request, Response response, Throwable
* throwable) { } });
*/
server.startRpcServer(new HostPort("127.0.0.1", 8080));
server.waitUntilShutdown();
}
}
public static void main(String[] args) throws Exception {
ResourceLeakDetector.setLevel(Level.DISABLED);
// CtClass.debugDump = "d:/debugDump";
try (TurboServer server = new TurboServer("shop", "auth");) {
Map<Class<?>, Object> services = Map.of(UserService.class, new UserServiceServerImpl());
server.registerService(services);
server.startRestServer(new HostPort("0.0.0.0", 8080));
server.waitUntilShutdown();
}
}
public static void main(String[] args) throws Exception {
ResourceLeakDetector.setLevel(Level.DISABLED);
try (TurboServer server = new TurboServer("shop", "auth");) {
Map<Class<?>, Object> services = Map.of(TurboUserService.class, new TurboUserServiceServerImpl());
server.registerService(services);
server.startRestServer(new HostPort("benchmark-server", 8080));
server.waitUntilShutdown();
}
}
public Client() {
ResourceLeakDetector.setLevel(Level.DISABLED);
client = new TurboClient("turbo-client.conf");
try {
client.register(TurboUserService.class);
userService = client.getService(TurboUserService.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Please note as the leak detection levels get higher, the tradeoff is a <strong>substantial</strong>
* performance loss. {@code PARANOID} should <strong>never</strong> be used in a production environment.
*/
public Level resourceLeakDetection() {
switch (runtimeMode) {
case PRODUCTION:
return Level.DISABLED;
case BENCHMARK:
return Level.SIMPLE;
case DEVELOPMENT:
return Level.PARANOID;
default:
throw new IllegalStateException("Invalid runtime mode!");
}
}
/**
* Initializes this network handler effectively preparing the server to
* listen for connections and handle network events.
*
* @param port
* the port that this network will be bound to.
* @throws Exception
* if any issues occur while starting the network.
*/
public void initialize(int port) throws IOException {
if (port != 43594 && port != 5555 && port != 43595)
logger.warning("The preferred ports for Runescape servers are 43594, 5555, and 43595!");
ResourceLeakDetector.setLevel(Server.DEBUG ? Level.PARANOID : NetworkConstants.RESOURCE_DETECTION);
bootstrap.group(loopGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(channelInitializer);
bootstrap.bind(port).syncUninterruptibly();
}
@Override
public boolean handle(CommandSender sender, String[] args) {
if (ResourceLeakDetector.isEnabled()) {
ResourceLeakDetector.setLevel(Level.DISABLED);
sender.sendMessage(ChatColor.YELLOW + "Disabled leak detector");
} else {
ResourceLeakDetector.setLevel(Level.PARANOID);
sender.sendMessage(ChatColor.YELLOW + "Enabled leak detector");
}
return true;
}
@Ignore("Used for checking for transport level leaks, my be unstable on CI.")
@Test(timeout = 60 * 1000)
public void testSendToClosedTransportFailsButDoesNotLeak() throws Exception {
Transport transport = null;
ResourceLeakDetector.setLevel(Level.PARANOID);
try (NettyEchoServer server = createEchoServer(createServerOptions())) {
server.start();
int port = server.getServerPort();
URI serverLocation = new URI("tcp://localhost:" + port);
for (int i = 0; i < 256; ++i) {
transport = createTransport(serverLocation, testListener, createClientOptions());
try {
transport.connect(null, null);
LOG.info("Connected to server:{} as expected.", serverLocation);
} catch (Exception e) {
fail("Should have connected to the server at " + serverLocation + " but got exception: " + e);
}
assertTrue(transport.isConnected());
ByteBuf sendBuffer = transport.allocateSendBuffer(10 * 1024 * 1024);
sendBuffer.writeBytes(new byte[] {0, 1, 2, 3, 4});
transport.close();
try {
transport.writeAndFlush(sendBuffer);
fail("Should throw on send of closed transport");
} catch (IOException ex) {
}
}
System.gc();
}
}
@Before
public void setUp() throws Exception {
ResourceLeakDetector.setLevel(Level.PARANOID);
// Use MockAltsHandshakerStub for all the tests.
AltsHandshakerOptions handshakerOptions = new AltsHandshakerOptions(null);
MockAltsHandshakerStub clientStub = new MockAltsHandshakerStub();
MockAltsHandshakerStub serverStub = new MockAltsHandshakerStub();
client = new AltsHandshakerClient(clientStub, handshakerOptions);
server = new AltsHandshakerClient(serverStub, handshakerOptions);
}
@Before
public void setUp() {
ResourceLeakDetector.setLevel(Level.PARANOID);
}
@Before
public void setUp() throws GeneralSecurityException {
ResourceLeakDetector.setLevel(Level.PARANOID);
client = new AltsChannelCrypter(new byte[AltsChannelCrypter.getKeyLength()], true);
server = new AltsChannelCrypter(new byte[AltsChannelCrypter.getKeyLength()], false);
}
@Before
public void setUp() {
ResourceLeakDetector.setLevel(Level.PARANOID);
}
@Override
public void afterPropertiesSet() throws Exception {
if ("dev".equalsIgnoreCase(active.trim())) {
ResourceLeakDetector.setLevel(Level.ADVANCED);
}
}
public static void main(String[] args) throws Exception {
ResourceLeakDetector.setLevel(Level.DISABLED);
SpringApplication.run(Server.class, args);
}
private void resetNettyLeakDetectionLevel() {
System.clearProperty(NETTY_LEAK_DETECTION_LEVEL_SYSTEM_PROP_KEY);
ResourceLeakDetector.setLevel(Level.SIMPLE);
}
@DataProvider(value = {
// no-op case
"null | null | null | null",
// cases showing that system property takes precedence over everything
"PARANOID | null | null | PARANOID",
"disabled | PARANOID | null | DISABLED", // also - lowercase works
"aDvAnCeD | PARANOID | DISABLED | ADVANCED", // also - mixed case works
// cases showing that NETTY_LEAK_DETECTION_LEVEL_SYSTEM_PROP_KEY takes precedence
// over NETTY_LEAK_DETECTION_LEVEL_APP_PROP_KEY if the system property is absent
"null | ADVANCED | null | ADVANCED",
"null | aDvAnCeD | PARANOID | ADVANCED", // yes, lower/mixed case still works here too
// cases showing NETTY_LEAK_DETECTION_LEVEL_APP_PROP_KEY will be used if the other
// options are not available
"null | null | DISABLED | DISABLED",
"null | null | pArAnOiD | PARANOID", // yes, lower/mixed case still works here too
}, splitBy = "\\|")
@Test
public void setupNettyLeakDetectionLevel_works_as_expected(
String systemPropValue, String configValueForSystemPropKey, String configValueForAppPropKey, Level expectedFinalLevel
) {
// given
assertThat(ResourceLeakDetector.getLevel()).isEqualTo(Level.SIMPLE);
assertThat(expectedFinalLevel).isNotEqualTo(Level.SIMPLE);
setSystemPropWithNullSupport(NETTY_LEAK_DETECTION_LEVEL_SYSTEM_PROP_KEY, systemPropValue);
Function<String, String> propertyExtractionFunction = (key) -> {
switch(key) {
case NETTY_LEAK_DETECTION_LEVEL_SYSTEM_PROP_KEY:
return configValueForSystemPropKey;
case NETTY_LEAK_DETECTION_LEVEL_APP_PROP_KEY:
return configValueForAppPropKey;
default:
throw new IllegalArgumentException("Unhandled config key: " + key);
}
};
Function<String, Boolean> hasPropertyFunction = (key) -> (propertyExtractionFunction.apply(key) != null);
// when
MainClassUtils.setupNettyLeakDetectionLevel(hasPropertyFunction, propertyExtractionFunction);
// then
if (expectedFinalLevel == null) {
// We expect that the method did nothing since it couldn't find anything to set
assertThat(System.getProperty(NETTY_LEAK_DETECTION_LEVEL_SYSTEM_PROP_KEY)).isNull();
assertThat(ResourceLeakDetector.getLevel()).isEqualTo(Level.SIMPLE);
}
else {
assertThat(System.getProperty(NETTY_LEAK_DETECTION_LEVEL_SYSTEM_PROP_KEY))
.isEqualTo(expectedFinalLevel.name());
assertThat(ResourceLeakDetector.getLevel()).isEqualTo(expectedFinalLevel);
}
}
@Before
public void setUp() {
ResourceLeakDetector.setLevel(Level.PARANOID);
}
@Before
public void setUp() throws GeneralSecurityException {
ResourceLeakDetector.setLevel(Level.PARANOID);
client = new AltsChannelCrypter(new byte[AltsChannelCrypter.getKeyLength()], true);
server = new AltsChannelCrypter(new byte[AltsChannelCrypter.getKeyLength()], false);
}
@Before
public void setUp() {
ResourceLeakDetector.setLevel(Level.PARANOID);
}