Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2019 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.hacep.consumer;
import java.util.Queue;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.hacep.EnvConfig;
import org.kie.hacep.core.GlobalStatus;
import org.kie.hacep.core.KieSessionContext;
import org.kie.hacep.core.infra.DefaultSessionSnapShooter;
import org.kie.hacep.core.infra.SnapshotInfos;
import org.kie.hacep.core.infra.consumer.ConsumerHandler;
import org.kie.hacep.core.infra.consumer.ItemToProcess;
import org.kie.hacep.core.infra.election.State;
import org.kie.hacep.core.infra.utils.SnapshotOnDemandUtils;
import org.kie.hacep.message.ControlMessage;
import org.kie.hacep.util.PrinterUtil;
import org.kie.remote.DroolsExecutor;
import org.kie.remote.command.RemoteCommand;
import org.kie.remote.command.VisitableCommand;
import org.kie.remote.impl.producer.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.kie.remote.util.SerializationUtil.deserialize;
public class DroolsConsumerHandler implements ConsumerHandler {
private static final Logger logger = LoggerFactory.getLogger(DroolsConsumerHandler.class);
private Logger loggerForTest;
private Producer producer;
private DefaultSessionSnapShooter snapshooter;
private EnvConfig config;
private KieSessionContext kieSessionContext;
private CommandHandler commandHandler;
private SnapshotInfos infos;
private boolean shutdown;
public DroolsConsumerHandler(Producer producer, EnvConfig envConfig) {
this.config = envConfig;
this.snapshooter = new DefaultSessionSnapShooter(config);
initializeKieSessionFromSnapshot(config);
this.producer = producer;
commandHandler = new CommandHandler(kieSessionContext, config, producer, snapshooter);
if (config.isUnderTest()) {
loggerForTest = PrinterUtil.getKafkaLoggerForTest(envConfig);
}
}
private void initializeKieSessionFromSnapshot(EnvConfig config) {
if(config.isSkipOnDemanSnapshot()) {// if true we reads the snapshots and wait until the first leaderElectionUpdate
this.infos = snapshooter.deserialize();
this.kieSessionContext = createSessionHolder(infos);
} else{
kieSessionContext = new KieSessionContext();
createClasspathSession( kieSessionContext );
}
}
public boolean initializeKieSessionFromSnapshotOnDemand(EnvConfig config) {
if(!config.isSkipOnDemanSnapshot()) {// if true we reads the snapshots and wait until the first leaderElectionUpdate
this.infos = SnapshotOnDemandUtils.askASnapshotOnDemand(config, snapshooter);
this.kieSessionContext = createSessionHolder(infos);
return true;
}
return false;
}
public DefaultSessionSnapShooter getSnapshooter(){
return snapshooter;
}
@Override
public void process( ItemToProcess item, State state) {
RemoteCommand command = deserialize((byte[])item.getObject());
process( command, state );
}
@Override
public void process( RemoteCommand command, State state ) {
if(config.isUnderTest()) { loggerForTest.warn("DroolsConsumerHandler.process Remote command on process:{} state:{}", command, state); }
if (state.equals(State.LEADER)) {
processCommand( command, state );
Queue