Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JBPM-9706] Create Native SQL command to operate database to execute …
…functions / SQL queries and stores in the execution results
- Loading branch information
1 parent
21d998e
commit 8f142d2
Showing
4 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
...rvices/jbpm-executor/src/main/java/org/jbpm/executor/commands/ExecuteSQLQueryCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2021 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.jbpm.executor.commands; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import org.jbpm.process.core.timer.DateTimeUtils; | ||
import org.jbpm.runtime.manager.impl.jpa.EntityManagerFactoryManager; | ||
import org.jbpm.shared.services.impl.TransactionalCommandService; | ||
import org.jbpm.shared.services.impl.commands.NativeQueryStringCommand; | ||
import org.jbpm.shared.services.impl.commands.QueryStringCommand; | ||
import org.kie.api.executor.Command; | ||
import org.kie.api.executor.CommandContext; | ||
import org.kie.api.executor.ExecutionResults; | ||
import org.kie.api.executor.Reoccurring; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
public class ExecuteSQLQueryCommand implements Command, Reoccurring { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ExecuteSQLQueryCommand.class); | ||
|
||
private long nextScheduleTimeAdd = 1 * 60 * 60 * 1000; // one hour in milliseconds | ||
|
||
@Override | ||
public Date getScheduleTime() { | ||
if (nextScheduleTimeAdd < 0) { | ||
return null; | ||
} | ||
|
||
long current = System.currentTimeMillis(); | ||
|
||
Date nextSchedule = new Date(current + nextScheduleTimeAdd); | ||
logger.debug("Next schedule for job {} is set to {}", this.getClass().getSimpleName(), nextSchedule); | ||
|
||
return nextSchedule; | ||
} | ||
|
||
@Override | ||
public ExecutionResults execute(CommandContext ctx) throws Exception { | ||
|
||
computeNextScheduleTime(ctx); | ||
EntityManagerFactory emf = getEntityManager(ctx); | ||
TransactionalCommandService commandService = new TransactionalCommandService(emf); | ||
|
||
String sql = (String) ctx.getData("SQL"); | ||
String paramsString = (String) ctx.getData("ParametersList"); | ||
|
||
Map<String, Object> parameters = new HashMap<>(); | ||
if(paramsString != null && !paramsString.isEmpty()) { | ||
String []p = paramsString.split(","); | ||
Arrays.stream(p).forEach(item -> parameters.put(item, ctx.getData(item))); | ||
} | ||
|
||
List<Object> data = commandService.execute(new NativeQueryStringCommand(sql, parameters)); | ||
|
||
ExecutionResults executionResults = new ExecutionResults(); | ||
executionResults.setData("size", data.size()); | ||
|
||
StringBuilder report = new StringBuilder(); | ||
for(Object item : data) { | ||
if(item instanceof Object[]) { | ||
for(Object cell : (Object[]) item) { | ||
report.append(cell).append(","); | ||
} | ||
} else { | ||
report.append(item); | ||
} | ||
report.append("\n"); | ||
} | ||
|
||
executionResults.setData("data", report.toString()); | ||
return executionResults; | ||
} | ||
|
||
private EntityManagerFactory getEntityManager(CommandContext ctx) { | ||
String emfName = (String) ctx.getData("EmfName"); | ||
if (emfName == null) { | ||
emfName = "org.jbpm.domain"; | ||
} | ||
return EntityManagerFactoryManager.get().getOrCreate(emfName); | ||
} | ||
|
||
private void computeNextScheduleTime(CommandContext ctx) { | ||
String singleRun = (String) ctx.getData("SingleRun"); | ||
if ("true".equalsIgnoreCase(singleRun)) { | ||
// disable rescheduling | ||
this.nextScheduleTimeAdd = -1; | ||
} | ||
String nextRun = (String) ctx.getData("NextRun"); | ||
if (nextRun != null) { | ||
nextScheduleTimeAdd = DateTimeUtils.parseDateAsDuration(nextRun); | ||
} | ||
} | ||
} |
183 changes: 183 additions & 0 deletions
183
jbpm-services/jbpm-executor/src/test/java/org/jbpm/executor/ExecuteSQLQueryCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright 2017 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.jbpm.executor; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.UUID; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
|
||
import org.apache.commons.lang3.mutable.MutableObject; | ||
import org.jbpm.executor.impl.ExecutorServiceImpl; | ||
import org.jbpm.executor.test.CountDownAsyncJobListener; | ||
import org.jbpm.test.persistence.util.PersistenceUtil; | ||
import org.jbpm.test.util.ExecutorTestUtil; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.kie.api.executor.CommandContext; | ||
import org.kie.api.executor.ExecutionResults; | ||
import org.kie.api.executor.ExecutorService; | ||
import org.kie.api.executor.RequestInfo; | ||
import org.kie.api.executor.STATUS; | ||
import org.kie.api.runtime.query.QueryContext; | ||
import org.kie.test.util.db.PoolingDataSourceWrapper; | ||
|
||
|
||
public class ExecuteSQLQueryCommandTest{ | ||
|
||
public static final Map<String, Object> cachedEntities = new HashMap<>(); | ||
private ExecutorService executorService; | ||
|
||
private static Properties dsProps; | ||
|
||
private EntityManagerFactory emf = null; | ||
|
||
private PoolingDataSourceWrapper pds; | ||
|
||
@BeforeClass | ||
public static void createDBServer() { | ||
dsProps = ExecutorTestUtil.getDatasourceProperties(); | ||
dsProps.setProperty("url", "jdbc:h2:tcp://localhost:9123/target/jbpm-exec-test;MVCC=TRUE"); | ||
dsProps.setProperty("tcpPort", "9123"); | ||
PersistenceUtil.startH2TcpServer(dsProps); | ||
} | ||
|
||
@AfterClass | ||
public static void stopDBServer() { | ||
PersistenceUtil.stopH2TcpServer(); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
pds = PersistenceUtil.setupPoolingDataSource(dsProps, "jdbc/jbpm-ds"); | ||
emf = Persistence.createEntityManagerFactory("org.jbpm.executor"); | ||
|
||
executorService = ExecutorServiceFactory.newExecutorService(emf); | ||
|
||
executorService.setThreadPoolSize(1); | ||
executorService.setInterval(3); | ||
|
||
executorService.init(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
executorService.clearAllRequests(); | ||
executorService.clearAllErrors(); | ||
|
||
System.clearProperty("org.kie.executor.msg.length"); | ||
System.clearProperty("org.kie.executor.stacktrace.length"); | ||
executorService.destroy(); | ||
if (emf != null) { | ||
emf.close(); | ||
} | ||
pds.close(); | ||
} | ||
|
||
|
||
protected CountDownAsyncJobListener configureListener(int threads) { | ||
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(threads); | ||
((ExecutorServiceImpl) executorService).addAsyncJobListener(countDownListener); | ||
|
||
return countDownListener; | ||
} | ||
|
||
@Test(timeout=60000) | ||
public void testSQLCommand() throws InterruptedException, ClassNotFoundException, IOException { | ||
String businessKey = UUID.randomUUID().toString(); | ||
|
||
CountDownAsyncJobListener countDownListener = configureListener(1); | ||
CommandContext ctxCMDprint = new CommandContext(); | ||
ctxCMDprint.setData("SingleRun", "true"); | ||
ctxCMDprint.setData("EmfName", "org.jbpm.executor"); | ||
ctxCMDprint.setData("businessKey", businessKey); | ||
ctxCMDprint.setData("SQL", "SELECT POWER(2,4)"); | ||
executorService.scheduleRequest("org.jbpm.executor.commands.ExecuteSQLQueryCommand", ctxCMDprint); | ||
countDownListener.waitTillCompleted(); | ||
|
||
CommandContext ctxCMD = new CommandContext(); | ||
ctxCMD.setData("SingleRun", "true"); | ||
ctxCMD.setData("EmfName", "org.jbpm.executor"); | ||
ctxCMD.setData("businessKey", businessKey); | ||
ctxCMD.setData("SQL", "SELECT * FROM RequestInfo WHERE businessKey = :paramKey"); | ||
ctxCMD.setData("ParametersList", "paramKey"); | ||
ctxCMD.setData("paramKey", businessKey); | ||
|
||
|
||
|
||
MutableObject<byte[]> data = new MutableObject<byte[]>(); | ||
((ExecutorServiceImpl) executorService).addAsyncJobListener(new AsynchronousJobListener() { | ||
|
||
@Override | ||
public void beforeJobScheduled(AsynchronousJobEvent event) { | ||
} | ||
|
||
@Override | ||
public void beforeJobExecuted(AsynchronousJobEvent event) { | ||
} | ||
|
||
@Override | ||
public void beforeJobCancelled(AsynchronousJobEvent event) { | ||
} | ||
|
||
@Override | ||
public void afterJobScheduled(AsynchronousJobEvent event) { | ||
} | ||
|
||
@Override | ||
public void afterJobExecuted(AsynchronousJobEvent event) { | ||
data.setValue(event.getJob().getResponseData()); | ||
} | ||
|
||
@Override | ||
public void afterJobCancelled(AsynchronousJobEvent event) { | ||
} | ||
}); | ||
Date futureDate = new Date(System.currentTimeMillis()); | ||
|
||
countDownListener = configureListener(1); | ||
executorService.scheduleRequest("org.jbpm.executor.commands.ExecuteSQLQueryCommand", futureDate, ctxCMD); | ||
countDownListener.waitTillCompleted(); | ||
|
||
List<RequestInfo> requests = executorService.getRequestsByBusinessKey(businessKey, new QueryContext()); | ||
assertNotNull(requests); | ||
assertEquals(2, requests.size()); | ||
assertEquals(STATUS.DONE, requests.get(0).getStatus()); | ||
|
||
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data.getValue()))) { | ||
ExecutionResults results = (ExecutionResults) ois.readObject(); | ||
String items = (String) results.getData().get("data"); | ||
assertFalse(items.isEmpty()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rvices/src/main/java/org/jbpm/shared/services/impl/commands/NativeQueryStringCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 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.jbpm.shared.services.impl.commands; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jbpm.shared.services.impl.JpaPersistenceContext; | ||
import org.kie.api.command.ExecutableCommand; | ||
import org.kie.api.runtime.Context; | ||
|
||
public class NativeQueryStringCommand implements ExecutableCommand<List<Object>> { | ||
|
||
private static final long serialVersionUID = -4014807273522465028L; | ||
|
||
private String queryName; | ||
private Map<String, Object> params; | ||
|
||
public NativeQueryStringCommand(String queryName) { | ||
this.queryName = queryName; | ||
} | ||
|
||
public NativeQueryStringCommand(String queryName, Map<String, Object> params) { | ||
this.queryName = queryName; | ||
this.params = params; | ||
} | ||
|
||
@Override | ||
public List<Object> execute(Context context) { | ||
JpaPersistenceContext ctx = (JpaPersistenceContext) context; | ||
return (List<Object>) ctx.nativeQueryStringWithParametersInTransaction(queryName, params == null ? new HashMap<>() : params, List.class); | ||
} | ||
|
||
} |