The net. sz. framework allows you to easily build services-so that you can focus more on logical functions-a preliminary study to easily cultivate children's attention

Source: Internet
Author: User

The net. sz. framework allows you to easily build services-so that you can focus more on logical functions-a preliminary study to easily cultivate children's attention
Preface

In previous articles, I have explained threadmodel, socket tcp, socket http, log, astart, and scripts;

These are all part-based explanations. Starting from today, we will take a look at the net. sz. framework;

The net. sz. framework is divided into java and C #. net versions. The two languages maintain high consistency;

What can net. sz. framework do?

Net. sz. framework is an underlying framework for online project verification after years of practice and research. It is committed to solving framework problems;

It makes it easier, pleasant, fast, and convenient for you to develop your own servers (games, microservices, webapis, data centers, transit centers, data integration centers, im servers, etc );

This allows you to easily process multiple threads, thread pools, and thread control without having to consider the thread security of multiple threads;

So that you don't have to worry too much about socket transmission issues, socket stick packages, subcontracting, subcontracting everything;

It allows you to easily build and implement tcp-based services and http-based services;

Allows you to easily implement any timer and any scheduled trigger in the program;

This allows you to use. java and. cs files as scripts to restore familiar scripts without learning other scripting languages;

Allows you to easily process mysql, sqlite (other data sources can be expanded), data storage, and read shortcuts;

It allows you to easily solve the log storage structure of highly concurrent files and record complete log data;

If the default parameters of log processing are modified, you can directly call the attribute settings or use the configuration file to modify them.

Add the file szlogger. ini to the other source root directory of the project.

1 ########## log information configuration ########## 2 # log-level case-insensitive debug: debug information info: normal information WARN: Warning Information ERROR: ERROR message FATAL: critical ERROR information 3 LOG_LEVEL = debug 4 # Whether to output to the console (false by default) 5 lele_print = true 6 # Whether to output to a file (true by default) 7 CONSOLE_FILE = true 8 # Whether to output to a file (true by default) 9 lele_file_buffer = true10 # log file storage path 11 LOG_PATH = .. /log/sz. log

 

S

Holle world

First open the NetBeans Tool

Create a project named net. sz. blog. test.

Then, create the project net. sz. blog. test-scipts in the same way.

Remember that the Project Name of the script project is the main project name + (-scripts); the conventions for reading script files;

After the project is created, we call the method to add references. Add the Framework library reference as needed;

 

Directory structure of the main project and script project. The script project depends on the main project.

This article focuses on script and timer tasks;

We now create a main package under the main project and create an iscript package under the main package.

We create the main function script interface.

1 package net. sz. blog. test. main. iseries; 2 3 import net. sz. framework. scripts. IInitBaseScript; 4 5/** 6 * main script 7 * <br> 8 * author lost programmers <br> 9 * blog http://www.cnblogs.com/ty408/ <br> 10 * mail 492794628@qq.com <br> 11 * phone 13882122019 <br> 12 */13 public interface IAppManagerScript extends IInitBaseScript {14 15/* Call The Script Function */16 void startEnd () after the program is started (); 17 18/* script called when the server needs to be stopped */19 void stop (); 20}
View Code

 

Create the main class Test_App_Manager under the main package.

1 package net. sz. blog. test. main; 2 3 import java. util. arrayList; 4 import net. sz. blog. test. main. iseries. IAppManagerScript; 5 import net. sz. framework. scripts. manager. scriptManager; 6 import net. sz. framework. szlog. szLogger; 7 8/* 9*10 * <br> 11 * author lost programmers <br> 12 * blog http://www.cnblogs.com/ty408/ <br> 13 * mail 492794628@qq.com <br> 14 * phone 13882122019 <br> 15 */16 public class Test_App_Manager {17 18 private static SzLogger log = SzLogger. getLogger (); 19/* Singleton script, which is used for reverse registration, instead of calling */20 private static IAppManagerScript iAppManagerScript = null; 21 22 public static IAppManagerScript getiAppManagerScript () {23 return iAppManagerScript; 24} 25 26 public static void setiAppManagerScript (IAppManagerScript iAppManagerScript) {27 Test_App_Manager.iAppManagerScript = iAppManagerScript; 28} 29 30 public stat Ic void main (String [] args) {31 32/* load all script files. By default, all problems under the project path-scripts file are loaded */33 ArrayList <String> loadScripts = ScriptManager. getInstance (). reload (); 34 String join = String. join (",", loadScripts); 35 log. error ("Script Loading Result:" + join); 36 37 if (iAppManagerScript! = Null) {38/* call the startup script */39 iAppManagerScript. startEnd (); 40} 41 42} 43}

 

We add a reference to the main project startup function script interface in the main class, waiting for injection when loading the script;

Add the AppManagerScript file to the main package under the script project.

1 package net. sz. blog. test. scripts. main; 2 3 import net. sz. blog. test. main. test_App_Manager; 4 import net. sz. blog. test. main. iseries. IAppManagerScript; 5 import net. sz. framework. szlog. szLogger; 6 7/** 8*9 * <br> 10 * author lost programmers <br> 11 * blog http://www.cnblogs.com/ty408/ <br> 12 * mail 492794628@qq.com <br> 13 * phone 13882122019 <br> 14 */15 public class AppManagerScript implements IAppManagerScript {1 6 17 private static SzLogger log = SzLogger. getLogger (); 18 19 @ Override20 public void _ init () {21/* reverse register the script back to the main project function */22 Test_App_Manager.setiAppManagerScript (this ); 23} 24 25 @ Override26 public void startEnd () {27 log. error ("main project Singleton mode script call function execution"); 28/* holle world! Print logs in the script */29 log. error ("holle world! "); 30} 31 32 @ Override33 public void stop () {34} 35 36}

 

Successfully completed the script registration and output the holle world

Timer task implementation

Create a timer package at the same level as the main package under the main project

Create the file ServerInfoTimer and execute the timer task that needs to be manually registered every second;

We temporarily Add the timer for public thread execution

1 package net. sz. blog. test. timer; 2 3 import net. sz. framework. szlog. szLogger; 4 import net. sz. framework. szthread. timerTaskModel; 5 6/** 7*8 * <br> 9 * author lost programmers <br> 10 * blog http://www.cnblogs.com/ty408/ <br> 11 * mail 492794628@qq.com <br> 12 * phone 13882122019 <br> 13 */14 public class ServerInfoTimer extends TimerTaskModel {15 16 private static SzLogger log = SzLogger. getLogger (); 17 18 public ServerInfoTimer () {19 super (1000);/* unlimited execution in one second */20} 21 22 @ Override23 public void run () {24 log. error ("I am executing a timer in the main project"); 25} 26 27}
View Code

 

Next, we create timer packages at the same level as main in the script project.

Create the TestTimerScript script file. The timer is still executed every second, but it is automatically executed without thread registration;

1 package net. sz. blog. test. scripts. timer; 2 3 import net. sz. framework. szlog. szLogger; 4 import net. sz. framework. szthread. timer. iseries. ISecondsEventTimerScript; 5 6/** 7*8 * <br> 9 * author lost programmers <br> 10 * blog http://www.cnblogs.com/ty408/ <br> 11 * mail 492794628@qq.com <br> 12 * phone 13882122019 <br> 13 */14 public class TestTimerScript implements ISecondsEventTimerScript {15 16 private static SzLogger log = SzLogger. getLogger (); 17 18/* automatic call */19 @ Override20 public void run (int sec) {21 log. error ("I am executing the timer in the script"); 22} 23 24}
View Code

 

Add the manual registration code of the ServerInfoTimer timer to the main method of the main function.

/* Actively register and select the timer task executed by the thread */ThreadPool. addTimerTask (ThreadPool. globalThread, new ServerInfoTimer ();/* Value Note: After the server program is ready to work, we must set this parameter because threads in the thread pool are divided into sys threads and user threads; without this identifier, the user thread will not execute the timer operation; */ThreadPool. setStarEnd (true );

 

Look at the effect first

 

The value of the two scheduled tasks is that the timer tasks in the script cannot take long time tasks, which will take up the execution time of other timer tasks. Generally, they are used as scheduled triggers;

By now, the basic use of net. sz. framework java is complete.

C # version code needs to be further integrated, so it will not be released for the time being;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.