Search in sources :

Example 1 with Event

use of com.blade.event.Event in project blade by biezhi.

the class EnvironmentWatcher method run.

@Override
public void run() {
    if (Const.CLASSPATH.endsWith(".jar")) {
        return;
    }
    final Path path = Paths.get(Const.CLASSPATH);
    try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
        path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
        // start an infinite loop
        while (true) {
            final WatchKey key = watchService.take();
            for (WatchEvent<?> watchEvent : key.pollEvents()) {
                final WatchEvent.Kind<?> kind = watchEvent.kind();
                if (kind == StandardWatchEventKinds.OVERFLOW) {
                    continue;
                }
                // get the filename for the event
                final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
                final String filename = watchEventPath.context().toString();
                // print it out
                if (log.isDebugEnabled()) {
                    log.debug("⬢ {} -> {}", kind, filename);
                }
                if (kind == StandardWatchEventKinds.ENTRY_DELETE && filename.startsWith(".app") && filename.endsWith(".properties.swp")) {
                    // reload env
                    log.info("⬢ Reload environment");
                    Environment environment = Environment.of("classpath:" + filename.substring(1, filename.length() - 4));
                    WebContext.blade().environment(environment);
                    // notify
                    WebContext.blade().eventManager().fireEvent(EventType.ENVIRONMENT_CHANGED, new Event().attribute("environment", environment));
                }
            }
            // reset the keyf
            boolean valid = key.reset();
            // deleted, for
            if (!valid) {
                break;
            }
        }
    } catch (IOException | InterruptedException ex) {
        log.error("Environment watch error", ex);
    }
}
Also used : IOException(java.io.IOException) Environment(com.blade.Environment) Event(com.blade.event.Event)

Example 2 with Event

use of com.blade.event.Event in project blade by biezhi.

the class SessionManager method destorySession.

/**
 * Remove a session
 *
 * @param session session instance
 */
public void destorySession(Session session) {
    session.attributes().clear();
    sessionMap.remove(session.id());
    Event event = new Event();
    event.attribute("session", session);
    eventManager.fireEvent(EventType.SESSION_DESTROY, event);
}
Also used : Event(com.blade.event.Event)

Example 3 with Event

use of com.blade.event.Event in project blade by biezhi.

the class NettyServer method startServer.

private void startServer(long startMs) throws Exception {
    ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED);
    boolean SSL = environment.getBoolean(ENV_KEY_SSL, false);
    // Configure SSL.
    SslContext sslCtx = null;
    if (SSL) {
        String certFilePath = environment.get(ENV_KEY_SSL_CERT, null);
        String privateKeyPath = environment.get(ENE_KEY_SSL_PRIVATE_KEY, null);
        String privateKeyPassword = environment.get(ENE_KEY_SSL_PRIVATE_KEY_PASS, null);
        log.info("{}SSL CertChainFile  Path: {}", getStartedSymbol(), certFilePath);
        log.info("{}SSL PrivateKeyFile Path: {}", getStartedSymbol(), privateKeyPath);
        sslCtx = SslContextBuilder.forServer(new File(certFilePath), new File(privateKeyPath), privateKeyPassword).build();
    }
    var bootstrap = new ServerBootstrap();
    int acceptThreadCount = environment.getInt(ENC_KEY_NETTY_ACCEPT_THREAD_COUNT, DEFAULT_ACCEPT_THREAD_COUNT);
    int ioThreadCount = environment.getInt(ENV_KEY_NETTY_IO_THREAD_COUNT, DEFAULT_IO_THREAD_COUNT);
    // enable epoll
    if (BladeKit.epollIsAvailable()) {
        log.info("{}Use EpollEventLoopGroup", getStartedSymbol());
        bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
        NettyServerGroup nettyServerGroup = EpollKit.group(acceptThreadCount, ioThreadCount);
        this.bossGroup = nettyServerGroup.getBoosGroup();
        this.workerGroup = nettyServerGroup.getWorkerGroup();
        bootstrap.group(bossGroup, workerGroup).channel(nettyServerGroup.getSocketChannel());
    } else {
        log.info("{}Use NioEventLoopGroup", getStartedSymbol());
        this.bossGroup = new NioEventLoopGroup(acceptThreadCount, new NamedThreadFactory("boss@"));
        this.workerGroup = new NioEventLoopGroup(ioThreadCount, new NamedThreadFactory("worker@"));
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);
    }
    scheduleEventLoop = new DefaultEventLoop();
    bootstrap.childHandler(new HttpServerInitializer(sslCtx, blade, scheduleEventLoop));
    String address = environment.get(ENV_KEY_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS);
    Integer port = environment.getInt(ENV_KEY_SERVER_PORT, DEFAULT_SERVER_PORT);
    channel = bootstrap.bind(address, port).sync().channel();
    String appName = environment.get(ENV_KEY_APP_NAME, "Blade");
    String url = Ansi.BgRed.and(Ansi.Black).format(" %s:%d ", address, port);
    String protocol = SSL ? "https" : "http";
    log.info("{}{} initialize successfully, Time elapsed: {} ms", getStartedSymbol(), appName, (System.currentTimeMillis() - startMs));
    log.info("{}Blade start with {}", getStartedSymbol(), url);
    log.info("{}Open browser access {}://{}:{} ⚡\r\n", getStartedSymbol(), protocol, address.replace(DEFAULT_SERVER_ADDRESS, LOCAL_IP_ADDRESS), port);
    blade.eventManager().fireEvent(EventType.SERVER_STARTED, new Event().attribute("blade", blade));
}
Also used : lombok.var(lombok.var) DefaultEventLoop(io.netty.channel.DefaultEventLoop) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(com.blade.event.Event) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 4 with Event

use of com.blade.event.Event in project blade by biezhi.

the class SessionManager method createSession.

/**
 * Add a session instance to sessionMap
 *
 * @param session session instance
 */
public void createSession(Session session) {
    sessionMap.put(session.id(), session);
    Event event = new Event();
    event.attribute("session", session);
    eventManager.fireEvent(EventType.SESSION_CREATED, event);
}
Also used : Event(com.blade.event.Event)

Aggregations

Event (com.blade.event.Event)4 Environment (com.blade.Environment)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 DefaultEventLoop (io.netty.channel.DefaultEventLoop)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SslContext (io.netty.handler.ssl.SslContext)1 File (java.io.File)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 lombok.var (lombok.var)1