外部控制不灵活,但是减少了实现难度。职责链上某一环上的具体实现不用考虑对下一环的调用,因为外部统一控制了。但是一般的外部控制也不能实现嵌套调用。如果有嵌套调用,并且希望由外部控制职责链的调用,关注公众号:搜云库技术团队,回复:“面试题”,获取,高清PDF:3625页互联网大厂面试题,实现起来会稍微复杂。具体可以参考 Spring Web Interceptor 机制的实现方法。
Apache Mina State Machine
Apache Mina 框架,虽然在 IO 框架领域不及 Netty,但它却提供了一个状态机的功能。https://mina.apache.org/mina-project/userguide/ch14-state-machine/ch14-state-machine.html。有自己实现状态机功能的同学可以参考其源码。
Spring State Machine
Spring 子项目众多,其中有个不显山不露水的状态机框架 —— Spring State Machine https://projects.spring.io/spring-statemachine/。可以通过 DSL 和注解两种方式定义。
double getPayAmount() {
double result;
if (_isDead) result = deadAmount();
else {
if (_isSeparated) result = separatedAmount();
else {
if (_isRetired) result = retiredAmount();
else result = normalPayAmount();
};
}
return result;
}
重构之后
double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
}
snmp抓到数据不是存储在mysql中,而是存在rrdtool生成的rrd文件中(在cacti根目录的rra文件夹下)。rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的档案文件(Round Robin Archive),它能够存储的数据笔数在创建时就已经定义。关于RRDTool的知识请参阅RRDTool教学。
RRDtool是指Round Robin Database 工具(环状数据库)。Round robin是一种处理定量数据、以及当前元素指针的技术。想象一个周边标有点的圆环--这些点就是时间存储的位置。从圆心画一条到圆周的某个点的箭头--这就是指针。就像我们在一个圆环上一样,没有起点和终点,你可以一直往下走下去。过来一段时间,所有可用的位置都会被用过,该循环过程会自动重用原来的位置。这样,数据集不会增大,并且不需要维护。RRDtool处理RRD数据库。它用向RRD数据库存储数据、从RRD数据库中提取数据。
[root@localhost ~]# wget http://www.cacti.net/downloads/cacti-0.8.8f.tar.gz
[root@localhost ~]# mkdir /web/vhosts -p
[root@localhost ~]# tar xf cacti-0.8.8f.tar.gz -C /web/vhosts/ #cacti根目录放置处
[root@localhost ~]# cd /web/vhosts/
[root@localhost vhosts]# ls
cacti-0.8.8f
[root@localhost vhosts]# ln -sv cacti-0.8.8f cacti
[root@localhost ~]# service httpd start
[root@localhost ~]# service mysqld start
cacti文件中有创建表的sql语句,但是没有创建数据库,需要手动创建。
[root@localhost cacti]# mysqladmin create cactidb
[root@localhost cacti]# mysql cactidb < cacti.sql [root@localhost cacti]# mysql -e "grant all on cactidb.* to cactiuser@localhost identified by 'cactiuser'" [root@localhost cacti]# mysqladmin flush-privileges [root@localhost cacti]# mysql -u cactiuser -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cactidb |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> \q
Bye