Search in sources :

Example 6 with DefaultIbisManager

use of nl.nn.adapterframework.unmanaged.DefaultIbisManager in project iaf by ibissource.

the class ShowScheduler method createSchedule.

private Response createSchedule(String groupName, String jobName, MultipartBody inputDataMap, boolean overwrite) {
    if (inputDataMap == null) {
        throw new ApiException("Missing post parameters");
    }
    String name = jobName;
    if (// If name is not explicitly set, try to deduct it from inputmap
    name == null)
        name = resolveStringFromMap(inputDataMap, "name");
    String cronExpression = resolveTypeFromMap(inputDataMap, "cron", String.class, "");
    int interval = resolveTypeFromMap(inputDataMap, "interval", Integer.class, -1);
    // Either one of the two has to be set
    if (interval == -1 && StringUtils.isEmpty(cronExpression)) {
        throw new ApiException("Either 'cron' or 'interval' has to be set");
    }
    String adapterName = resolveStringFromMap(inputDataMap, "adapter");
    // Make sure the adapter exists!
    DefaultIbisManager manager = (DefaultIbisManager) getIbisManager();
    IAdapter adapter = manager.getRegisteredAdapter(adapterName);
    if (adapter == null) {
        throw new ApiException("Adapter [" + adapterName + "] not found");
    }
    // Make sure the receiver exists!
    String receiverName = resolveStringFromMap(inputDataMap, "receiver");
    Receiver<?> receiver = adapter.getReceiverByName(receiverName);
    if (receiver == null) {
        throw new ApiException("Receiver [" + receiverName + "] not found");
    }
    String listenerName = null;
    IListener<?> listener = receiver.getListener();
    if (listener != null) {
        listenerName = listener.getName();
    }
    if (StringUtils.isEmpty(listenerName)) {
        throw new ApiException("unable to determine listener for receiver [" + receiverName + "]");
    }
    Configuration applicationContext = adapter.getConfiguration();
    String jobGroup = groupName;
    if (StringUtils.isEmpty(jobGroup)) {
        jobGroup = adapter.getConfiguration().getName();
    }
    boolean persistent = resolveTypeFromMap(inputDataMap, "persistent", boolean.class, false);
    boolean hasLocker = resolveTypeFromMap(inputDataMap, "locker", boolean.class, false);
    String lockKey = resolveTypeFromMap(inputDataMap, "lockkey", String.class, "lock4[" + name + "]");
    String message = resolveStringFromMap(inputDataMap, "message");
    String description = resolveStringFromMap(inputDataMap, "description");
    SchedulerHelper sh = getSchedulerHelper();
    // First try to create the schedule and run it on the local ibis before storing it in the database
    DatabaseJob jobdef = SpringUtils.createBean(applicationContext, DatabaseJob.class);
    jobdef.setCronExpression(cronExpression);
    jobdef.setName(name);
    jobdef.setAdapterName(adapterName);
    jobdef.setJavaListener(listenerName);
    jobdef.setJobGroup(jobGroup);
    jobdef.setMessage(message);
    jobdef.setDescription(description);
    jobdef.setInterval(interval);
    if (hasLocker) {
        Locker locker = SpringUtils.createBean(applicationContext, Locker.class);
        locker.setName(lockKey);
        locker.setObjectId(lockKey);
        locker.setDatasourceName(JndiDataSourceFactory.GLOBAL_DEFAULT_DATASOURCE_NAME);
        jobdef.setLocker(locker);
    }
    try {
        jobdef.configure();
        sh.scheduleJob(jobdef);
    } catch (Exception e) {
        throw new ApiException("Failed to add schedule", e);
    }
    // Save the job in the database
    if (persistent && AppConstants.getInstance().getBoolean("loadDatabaseSchedules.active", false)) {
        boolean success = false;
        FixedQuerySender qs = SpringUtils.createBean(applicationContext, FixedQuerySender.class);
        qs.setDatasourceName(JndiDataSourceFactory.GLOBAL_DEFAULT_DATASOURCE_NAME);
        qs.setQuery("SELECT COUNT(*) FROM IBISSCHEDULES");
        try {
            qs.configure();
        } catch (ConfigurationException e) {
            throw new ApiException("Error creating FixedQuerySender bean to store job in database", e);
        }
        try {
            qs.open();
            try (Connection conn = qs.getConnection()) {
                if (overwrite) {
                    String deleteQuery = "DELETE FROM IBISSCHEDULES WHERE JOBNAME=? AND JOBGROUP=?";
                    try (PreparedStatement deleteStatement = conn.prepareStatement(deleteQuery)) {
                        deleteStatement.setString(1, name);
                        deleteStatement.setString(2, jobGroup);
                        deleteStatement.executeUpdate();
                    }
                }
                String insertQuery = "INSERT INTO IBISSCHEDULES (JOBNAME, JOBGROUP, ADAPTER, RECEIVER, CRON, EXECUTIONINTERVAL, MESSAGE, DESCRIPTION, LOCKER, LOCK_KEY, CREATED_ON, BY_USER) " + "VALUES (?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?)";
                try (PreparedStatement stmt = conn.prepareStatement(insertQuery)) {
                    stmt.setString(1, name);
                    stmt.setString(2, jobGroup);
                    stmt.setString(3, adapterName);
                    stmt.setString(4, listenerName);
                    stmt.setString(5, cronExpression);
                    stmt.setInt(6, interval);
                    stmt.setClob(7, new StringReader(message));
                    stmt.setString(8, description);
                    stmt.setBoolean(9, hasLocker);
                    stmt.setString(10, lockKey);
                    stmt.setString(11, getUserPrincipalName());
                    success = stmt.executeUpdate() > 0;
                }
            }
        } catch (SenderException | SQLException | JdbcException e) {
            throw new ApiException("error saving job in database", e);
        } finally {
            qs.close();
        }
        if (!success)
            throw new ApiException("An error occurred while storing the job in the database");
    }
    return Response.status(Response.Status.CREATED).build();
}
Also used : Locker(nl.nn.adapterframework.util.Locker) Configuration(nl.nn.adapterframework.configuration.Configuration) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SenderException(nl.nn.adapterframework.core.SenderException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) SchedulerException(org.quartz.SchedulerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SchedulerHelper(nl.nn.adapterframework.scheduler.SchedulerHelper) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) DatabaseJob(nl.nn.adapterframework.scheduler.job.DatabaseJob) StringReader(java.io.StringReader) SenderException(nl.nn.adapterframework.core.SenderException) IAdapter(nl.nn.adapterframework.core.IAdapter) DefaultIbisManager(nl.nn.adapterframework.unmanaged.DefaultIbisManager) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 7 with DefaultIbisManager

use of nl.nn.adapterframework.unmanaged.DefaultIbisManager in project iaf by ibissource.

the class ClassLoaderManagerTest method createAdapter4ServiceClassLoader.

private static void createAdapter4ServiceClassLoader(String config4Adaptername) throws ConfigurationException {
    // Mock a configuration with an adapter in it
    IbisManager ibisManager = spy(new DefaultIbisManager());
    ibisManager.setIbisContext(ibisContext);
    Configuration configuration = new TestConfiguration();
    configuration.setName("dummyConfiguration");
    configuration.setVersion("1");
    configuration.setIbisManager(ibisManager);
    Adapter adapter = spy(new Adapter());
    adapter.setName(config4Adaptername);
    PipeLine pl = new PipeLine();
    pl.setFirstPipe("dummy");
    EchoPipe pipe = new EchoPipe();
    pipe.setName("dummy");
    pl.addPipe(pipe);
    PipeLineExit ple = new PipeLineExit();
    ple.setPath("success");
    ple.setState(ExitState.SUCCESS);
    pl.registerPipeLineExit(ple);
    adapter.setPipeLine(pl);
    doAnswer(new Answer<PipeLineResult>() {

        @Override
        public PipeLineResult answer(InvocationOnMock invocation) throws Throwable {
            PipeLineSession session = (PipeLineSession) invocation.getArguments()[2];
            URL file = this.getClass().getResource(JAR_FILE);
            session.put("configurationJar", Misc.streamToBytes(file.openStream()));
            return new PipeLineResult();
        }
    }).when(adapter).processMessage(anyString(), any(Message.class), any(PipeLineSession.class));
    adapter.setConfiguration(configuration);
    configuration.registerAdapter(adapter);
    ibisManager.addConfiguration(configuration);
    when(ibisContext.getIbisManager()).thenReturn(ibisManager);
}
Also used : DefaultIbisManager(nl.nn.adapterframework.unmanaged.DefaultIbisManager) TestConfiguration(nl.nn.adapterframework.testutil.TestConfiguration) Message(nl.nn.adapterframework.stream.Message) EchoPipe(nl.nn.adapterframework.pipes.EchoPipe) TestConfiguration(nl.nn.adapterframework.testutil.TestConfiguration) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) Adapter(nl.nn.adapterframework.core.Adapter) URL(java.net.URL) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeLine(nl.nn.adapterframework.core.PipeLine) DefaultIbisManager(nl.nn.adapterframework.unmanaged.DefaultIbisManager) PipeLineExit(nl.nn.adapterframework.core.PipeLineExit)

Aggregations

DefaultIbisManager (nl.nn.adapterframework.unmanaged.DefaultIbisManager)7 SchedulerHelper (nl.nn.adapterframework.scheduler.SchedulerHelper)6 SchedulerException (org.quartz.SchedulerException)6 Scheduler (org.quartz.Scheduler)5 RolesAllowed (javax.annotation.security.RolesAllowed)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Date (java.util.Date)2 PUT (javax.ws.rs.PUT)2 SchedulerAdapter (nl.nn.adapterframework.scheduler.SchedulerAdapter)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ServletException (javax.servlet.ServletException)1 DELETE (javax.ws.rs.DELETE)1 Configuration (nl.nn.adapterframework.configuration.Configuration)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1