Use Embedded Server

Updated at 1685685110000
Traditionally, to host EzyFox Server's applications and plugin, you installed a single EzyFox Server on your server, and pushed all of modules onto that one server. But in many cases you just have one service (one application and one plugin), so you will want to consist the single service along with a full EzyFox Server distribution, packaged together and compressed into a single JAR. So an embedded server is a better choice.

1. Add dependencies

<dependency>
    <groupId>com.tvd12</groupId>
    <artifactId>ezyfox-server-embedded</artifactId>
    <version>1.2.8.1</version>
</dependency>

You can find out the latest version on maven central

2. Setting an embedded server

2.1 Create a Plugin Entry

For more details, you can look at Hello World PluginEntry

    public static class HelloPluginEntry extends EzySimplePluginEntry {
        @Override
        protected String[] getScanableBeanPackages() {
            return new String[] {
                    "com.tvd12.ezyfoxserver.embedded.test" // replace by your package
            };
        }
    }

2.2 Create an App Entry

For more details, you can look at the Hello World AppEntry

    public static class HelloAppEntry extends EzySimpleAppEntry {
        @Override
        protected String[] getScanablePackages() {
            return new String[] {
                    "com.tvd12.ezyfoxserver.embedded.test" // replace by your package
            };
        }
    }

2.3 Setting

    EzyPluginSettingBuilder pluginSettingBuilder = new EzyPluginSettingBuilder()
            .name("hello")
            .addListenEvent(EzyEventType.USER_LOGIN)
            .entryLoader(HelloPluginEntryLoader.class);
    EzyAppSettingBuilder appSettingBuilder = new EzyAppSettingBuilder()
            .name("hello")
            .entryLoader(HelloAppEntryLoader.class);
    EzyZoneSettingBuilder zoneSettingBuilder = new EzyZoneSettingBuilder()
            .name("hello")
            .application(appSettingBuilder.build())
            .plugin(pluginSettingBuilder.build());
    EzySimpleSettings settings = new EzySettingsBuilder()
            .zone(zoneSettingBuilder.build())
            .build();

3. Create and Start an Embedded Server

    EzyEmbeddedServer server = EzyEmbeddedServer.builder()
            .settings(settings)
            .build();
    server.start();

4. An Example

For the completed example, you can look at HelloWorlStartup class

    import com.tvd12.ezyfox.bean.EzyBeanContext;
    import com.tvd12.ezyfoxserver.app.EzyAppRequestController;
    import com.tvd12.ezyfoxserver.constant.EzyEventType;
    import com.tvd12.ezyfoxserver.embedded.EzyEmbeddedServer;
    import com.tvd12.ezyfoxserver.ext.EzyAbstractAppEntryLoader;
    import com.tvd12.ezyfoxserver.ext.EzyAbstractPluginEntryLoader;
    import com.tvd12.ezyfoxserver.ext.EzyAppEntry;
    import com.tvd12.ezyfoxserver.ext.EzyPluginEntry;
    import com.tvd12.ezyfoxserver.setting.EzyAppSettingBuilder;
    import com.tvd12.ezyfoxserver.setting.EzyPluginSettingBuilder;
    import com.tvd12.ezyfoxserver.setting.EzySettingsBuilder;
    import com.tvd12.ezyfoxserver.setting.EzySimpleSettings;
    import com.tvd12.ezyfoxserver.setting.EzyZoneSettingBuilder;
    import com.tvd12.ezyfoxserver.support.controller.EzyUserRequestAppSingletonController;
    import com.tvd12.ezyfoxserver.support.entry.EzySimpleAppEntry;
    import com.tvd12.ezyfoxserver.support.entry.EzySimplePluginEntry;
    public class HelloEmbeddedServer {
        public static void main(String[] args) throws Exception {
            EzyPluginSettingBuilder pluginSettingBuilder = new EzyPluginSettingBuilder()
                    .name("hello")
                    .addListenEvent(EzyEventType.USER_LOGIN)
                    .entryLoader(HelloPluginEntryLoader.class);
            EzyAppSettingBuilder appSettingBuilder = new EzyAppSettingBuilder()
                    .name("hello")
                    .entryLoader(HelloAppEntryLoader.class);
            EzyZoneSettingBuilder zoneSettingBuilder = new EzyZoneSettingBuilder()
                    .name("hello")
                    .application(appSettingBuilder.build())
                    .plugin(pluginSettingBuilder.build());
            EzySimpleSettings settings = new EzySettingsBuilder()
                    .zone(zoneSettingBuilder.build())
                    .build();
            EzyEmbeddedServer server = EzyEmbeddedServer.builder()
                    .settings(settings)
                    .build();
            server.start();
        }
        public static class HelloAppEntry extends EzySimpleAppEntry {
            @Override
            protected String[] getScanablePackages() {
                return new String[] {
                        "com.tvd12.ezyfoxserver.embedded.test" // replace by your package
                };
            }
            @Override
            protected EzyAppRequestController newUserRequestController(EzyBeanContext beanContext) {
                return EzyUserRequestAppSingletonController.builder()
                        .beanContext(beanContext)
                        .build();
            }
        }
        public static class HelloAppEntryLoader extends EzyAbstractAppEntryLoader {
            @Override
            public EzyAppEntry load() throws Exception {
                return new HelloAppEntry();
            }
        }
        public static class HelloPluginEntry extends EzySimplePluginEntry {
            @Override
            protected String[] getScanableBeanPackages() {
                return new String[] {
                        "com.tvd12.ezyfoxserver.embedded.test.plugin" // replace by your package
                };
            }
        }
        public static class HelloPluginEntryLoader extends EzyAbstractPluginEntryLoader {
            @Override
            public EzyPluginEntry load() throws Exception {
                return new HelloPluginEntry();
            }
        }
    }

5. Take a test

Open the test file on your browser, enter you name and click the connect button. You can look at hello-world project for the completed source code of the example.

Next step

We will try to setup embedded server.