@Test public void testGetRouteSynchronousIfQueueFullAndCalledFromSameThread() throws Exception { final DefaultAsyncQueueFullPolicy router = new DefaultAsyncQueueFullPolicy(); assertEquals(EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), Level.ALL)); assertEquals(EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), Level.OFF)); } }
@Test public void testGetRouteEnqueuesIfQueueFullAndCalledFromDifferentThread() throws Exception { final DefaultAsyncQueueFullPolicy router = new DefaultAsyncQueueFullPolicy(); assertEquals(EventRoute.ENQUEUE, router.getRoute(otherThreadId(), Level.ALL)); assertEquals(EventRoute.ENQUEUE, router.getRoute(otherThreadId(), Level.OFF)); }
private static AsyncQueueFullPolicy createCustomRouter(final String router) { try { final Class<? extends AsyncQueueFullPolicy> cls = LoaderUtil.loadClass(router).asSubclass(AsyncQueueFullPolicy.class); LOGGER.debug("Creating custom AsyncQueueFullPolicy '{}'", router); return cls.newInstance(); } catch (final Exception ex) { LOGGER.debug("Using DefaultAsyncQueueFullPolicy. Could not create custom AsyncQueueFullPolicy '{}': {}", router, ex.toString()); return new DefaultAsyncQueueFullPolicy(); } }
/** * Creates and returns {@link AsyncQueueFullPolicy} instances based on user-specified system properties. * <p> * Property {@code "log4j2.AsyncQueueFullPolicy"} controls the routing behaviour. If this property is not specified or * has value {@code "Default"}, this method returns {@link DefaultAsyncQueueFullPolicy} objects. * </p> <p> * If this property has value {@code "Discard"}, this method returns {@link DiscardingAsyncQueueFullPolicy} objects. * </p> <p> * For any other value, this method interprets the value as the fully qualified name of a class implementing the * {@link AsyncQueueFullPolicy} interface. The class must have a default constructor. * </p> * * @return a new AsyncQueueFullPolicy */ public static AsyncQueueFullPolicy create() { final String router = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME_ASYNC_EVENT_ROUTER); if (router == null || PROPERTY_VALUE_DEFAULT_ASYNC_EVENT_ROUTER.equals(router) || DefaultAsyncQueueFullPolicy.class.getSimpleName().equals(router) || DefaultAsyncQueueFullPolicy.class.getName().equals(router)) { return new DefaultAsyncQueueFullPolicy(); } if (PROPERTY_VALUE_DISCARDING_ASYNC_EVENT_ROUTER.equals(router) || DiscardingAsyncQueueFullPolicy.class.getSimpleName().equals(router) || DiscardingAsyncQueueFullPolicy.class.getName().equals(router)) { return createDiscardingAsyncQueueFullPolicy(); } return createCustomRouter(router); }