protected Broadcaster getBroadcaster(boolean autoCreate) { if (broadcaster == null) { throw new IllegalStateException("No Broadcaster associated with this AtmosphereResource."); } String s = config.getInitParameter(ApplicationConfig.RECOVER_DEAD_BROADCASTER); if (s != null) { autoCreate = Boolean.parseBoolean(s); } if (autoCreate && broadcaster.isDestroyed() && config.getBroadcasterFactory() != null) { logger.debug( "Broadcaster {} has been destroyed and cannot be re-used. Recreating a new one with the same name. You can turn off this" + " mechanism by adding, in web.xml, {} set to false", broadcaster.getID(), ApplicationConfig.RECOVER_DEAD_BROADCASTER); Broadcaster.SCOPE scope = broadcaster.getScope(); synchronized (this) { String id = scope != Broadcaster.SCOPE.REQUEST ? broadcaster.getID() : broadcaster.getID() + ".recovered" + UUID.randomUUID(); // Another Thread may have added the Broadcaster. broadcaster = config.getBroadcasterFactory().lookup(id, true); broadcaster.setScope(scope); broadcaster.addAtmosphereResource(this); } } return broadcaster; }
/** * Create a {@link Meteor} using the {@link HttpServletRequest} and use a list of {@link * BroadcastFilter} and {@link Serializer} for writing the result of a broadcast operation the * {@link HttpServletResponse}. * * @param req an {@link HttpServletRequest} * @param scope the {@link Broadcaster.SCOPE}} * @param l a list of {@link BroadcastFilter} * @param s a {@link Serializer} used when writing broadcast events * @return a {@link Meteor} than can be used to resume, suspend and broadcast {@link Object} */ public static final Meteor build( HttpServletRequest req, Broadcaster.SCOPE scope, List<BroadcastFilter> l, Serializer s) { AtmosphereResource r = (AtmosphereResource) req.getAttribute(ATMOSPHERE_RESOURCE); if (r == null) throw new IllegalStateException("MeteorServlet not defined in web.xml"); Broadcaster b = null; if (scope == Broadcaster.SCOPE.REQUEST) { try { BroadcasterFactory f = r.getAtmosphereConfig().getBroadcasterFactory(); b = f.get( DefaultBroadcaster.class, DefaultBroadcaster.class.getSimpleName() + r.getAtmosphereConfig().uuidProvider().generateUuid()); } catch (Throwable t) { throw new RuntimeException(t); } b.setScope(scope); r.setBroadcaster(b); req.setAttribute(AtmosphereResourceImpl.SKIP_BROADCASTER_CREATION, Boolean.TRUE); } Meteor m = new Meteor(r, l, (s != null ? s : r.getSerializer())); req.setAttribute(METEOR, m); return m; }