You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
package vip.xumy.drools.pojo; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import org.drools.core.time.SessionPseudoClock; |
|
import org.kie.api.runtime.KieSession; |
|
|
|
//@Log4j2 |
|
public class DroolsBurster { |
|
private KieSession ks; |
|
private SessionPseudoClock clock; |
|
private long currentTime; |
|
private int burst; |
|
private int count; |
|
|
|
public DroolsBurster(KieSession ks, String clockType, int burst) { |
|
this.ks = ks; |
|
if (clockType == "pseudo") |
|
clock = ks.getSessionClock(); |
|
else |
|
clock = null; |
|
currentTime = 0; |
|
currentTime = System.currentTimeMillis(); |
|
clock.advanceTime(currentTime, TimeUnit.MILLISECONDS); |
|
count = 0; |
|
this.burst = burst; |
|
} |
|
|
|
// 将事件插入KieSession,并根据时间戳(ms)推进时钟,在一定时间间隔触发规则 |
|
public int insert(Object evt, long timestamp) { |
|
ks.insert(evt); |
|
if (count >= burst || timestamp - currentTime > 1000) { |
|
if (clock != null) { |
|
clock.advanceTime(timestamp - currentTime, TimeUnit.MILLISECONDS); |
|
currentTime = timestamp; |
|
} |
|
|
|
count = 0; |
|
int num = ks.fireAllRules(); |
|
return num; |
|
} |
|
count++; |
|
return 0; |
|
} |
|
|
|
public void dispose() { |
|
if (ks != null) |
|
ks.dispose(); |
|
} |
|
|
|
}
|
|
|