Search in sources :

Example 11 with FixedQuerySender

use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.

the class ClassLoaderManagerTest method mockDatabase.

private static void mockDatabase() throws Exception {
    // Mock a FixedQuerySender
    JmsRealm jmsRealm = spy(new JmsRealm());
    jmsRealm.setDatasourceName("fake");
    jmsRealm.setRealmName("myRealm");
    JmsRealmFactory.getInstance().registerJmsRealm(jmsRealm);
    FixedQuerySender fq = mock(FixedQuerySender.class);
    doReturn(new GenericDbmsSupport()).when(fq).getDbmsSupport();
    Connection conn = mock(Connection.class);
    doReturn(conn).when(fq).getConnection();
    PreparedStatement stmt = mock(PreparedStatement.class);
    doReturn(stmt).when(conn).prepareStatement(anyString());
    ResultSet rs = mock(ResultSet.class);
    doReturn(true).when(rs).next();
    doReturn("dummy").when(rs).getString(anyInt());
    URL file = ClassLoaderManager.class.getResource(JAR_FILE);
    doReturn(Misc.streamToBytes(file.openStream())).when(rs).getBytes(anyInt());
    doReturn(rs).when(stmt).executeQuery();
    doReturn(fq).when(ibisContext).createBeanAutowireByName(FixedQuerySender.class);
}
Also used : JmsRealm(nl.nn.adapterframework.jms.JmsRealm) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender) URL(java.net.URL)

Example 12 with FixedQuerySender

use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.

the class DatabaseClassLoaderTest method mockDatabase.

private void mockDatabase(boolean throwException) throws Exception {
    // Mock a FixedQuerySender
    FixedQuerySender fq = mock(FixedQuerySender.class);
    doReturn(new GenericDbmsSupport()).when(fq).getDbmsSupport();
    Connection conn = mock(Connection.class);
    doReturn(conn).when(fq).getConnection();
    PreparedStatement stmt = mock(PreparedStatement.class);
    doReturn(stmt).when(conn).prepareStatement(anyString());
    ResultSet rs = mock(ResultSet.class);
    doReturn(!throwException).when(rs).next();
    doReturn("dummy").when(rs).getString(anyInt());
    URL file = this.getClass().getResource(JAR_FILE);
    doReturn(Misc.streamToBytes(file.openStream())).when(rs).getBytes(anyInt());
    doReturn(rs).when(stmt).executeQuery();
    doReturn(fq).when(ibisContext).createBeanAutowireByName(FixedQuerySender.class);
    // IbisContext.log is a void method
    @SuppressWarnings("rawtypes") Answer answer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String message = invocation.getArgument(0);
            MessageKeeperLevel level = invocation.getArgument(1);
            Exception exception = invocation.getArgument(2);
            new ApplicationMessageEvent(spy(ApplicationContext.class), message, level, exception);
            return null;
        }
    };
    // Mock the IbisContext's log method which uses getApplicationContext which in turn creates a
    // new ApplicationContext if non exists. This functionality should be removed sometime in the future.
    // During testing, the IbisContext never initialises and thus there is no ApplicationContext. The
    // creation of the ApplicationContext during the test phase causes IllegalStateExceptions
    // In turn this causes the actual thing we want to test to never be 'hit', aka the log message.
    doAnswer(answer).when(ibisContext).log(anyString(), any(MessageKeeperLevel.class), any(Exception.class));
}
Also used : MessageKeeperLevel(nl.nn.adapterframework.util.MessageKeeper.MessageKeeperLevel) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) URL(java.net.URL) ClassLoaderException(nl.nn.adapterframework.configuration.ClassLoaderException) Answer(org.mockito.stubbing.Answer) ApplicationContext(org.springframework.context.ApplicationContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultSet(java.sql.ResultSet) ApplicationMessageEvent(nl.nn.adapterframework.lifecycle.ApplicationMessageEvent) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 13 with FixedQuerySender

use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.

the class TestQuerySenderMocks method testIfMockedResultSetWorks.

@Test
public void testIfMockedResultSetWorks() throws Exception {
    configuration = new TestConfiguration();
    ResultSet mockedResultSet = FixedQuerySenderMock.ResultSetBuilder.create().build();
    configuration.mockQuery("SELECT COUNT(*) FROM FAKE_DATABASE_TABLE", mockedResultSet);
    FixedQuerySender qs = SpringUtils.createBean(configuration, FixedQuerySender.class);
    qs.setQuery("SELECT COUNT(*) FROM FAKE_DATABASE_TABLE");
    qs.configure();
    qs.open();
    try (Connection conn = qs.getConnection()) {
        try (PreparedStatement stmt = conn.prepareStatement("SELECT JOBNAME,JOBGROUP,ADAPTER,RECEIVER,CRON,EXECUTIONINTERVAL,MESSAGE,LOCKER,LOCK_KEY FROM IBISSCHEDULES")) {
            try (ResultSet rs = stmt.executeQuery()) {
                assertTrue(rs == mockedResultSet);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender) Test(org.junit.Test)

Example 14 with FixedQuerySender

use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.

the class CleanupDatabaseJob method execute.

@Override
public void execute(IbisManager ibisManager) {
    Date date = new Date();
    int maxRows = AppConstants.getInstance().getInt("cleanup.database.maxrows", 25000);
    List<MessageLogObject> messageLogs = getAllMessageLogs(ibisManager);
    for (MessageLogObject mlo : messageLogs) {
        FixedQuerySender qs = null;
        try {
            qs = SpringUtils.createBean(getApplicationContext(), FixedQuerySender.class);
            qs.setDatasourceName(mlo.getDatasourceName());
            qs.setName("cleanupDatabase-" + mlo.getTableName());
            qs.setQueryType("other");
            qs.setTimeout(getQueryTimeout());
            qs.setScalar(true);
            Parameter param = new Parameter("now", DateUtils.format(date));
            param.setType(ParameterType.TIMESTAMP);
            qs.addParameter(param);
            String query = qs.getDbmsSupport().getCleanUpIbisstoreQuery(mlo.getTableName(), mlo.getKeyField(), mlo.getTypeField(), mlo.getExpiryDateField(), maxRows);
            qs.setQuery(query);
            qs.configure();
            qs.open();
            boolean deletedAllRecords = false;
            while (!deletedAllRecords) {
                Message result = qs.sendMessage(Message.nullMessage(), null);
                String resultString = result.asString();
                log.info("deleted [" + resultString + "] rows");
                int numberOfRowsAffected = Integer.valueOf(resultString);
                if (maxRows <= 0 || numberOfRowsAffected < maxRows) {
                    deletedAllRecords = true;
                } else {
                    log.info("executing the query again for job [cleanupDatabase]!");
                }
            }
        } catch (Exception e) {
            String msg = "error while deleting expired records from table [" + mlo.getTableName() + "] (as part of scheduled job execution): " + e.getMessage();
            getMessageKeeper().add(msg, MessageKeeperLevel.ERROR);
            log.error(getLogPrefix() + msg);
        } finally {
            if (qs != null) {
                qs.close();
            }
        }
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) Parameter(nl.nn.adapterframework.parameters.Parameter) Date(java.util.Date) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 15 with FixedQuerySender

use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.

the class CheckReloadJob method execute.

@Override
public void execute(IbisManager ibisManager) {
    if (ibisManager.getIbisContext().isLoadingConfigs()) {
        String msg = "skipping checkReload because one or more configurations are currently loading";
        getMessageKeeper().add(msg, MessageKeeperLevel.INFO);
        log.info(getLogPrefix() + msg);
        return;
    }
    List<String> configNames = new ArrayList<>();
    List<String> configsToReload = new ArrayList<>();
    FixedQuerySender qs = SpringUtils.createBean(getApplicationContext(), FixedQuerySender.class);
    qs.setDatasourceName(getDataSource());
    qs.setQuery("SELECT COUNT(*) FROM IBISCONFIG");
    String booleanValueTrue = qs.getDbmsSupport().getBooleanValue(true);
    String selectQuery = "SELECT VERSION FROM IBISCONFIG WHERE NAME=? AND ACTIVECONFIG = " + booleanValueTrue + " and AUTORELOAD = " + booleanValueTrue;
    try {
        qs.configure();
        qs.open();
        try (Connection conn = qs.getConnection();
            PreparedStatement stmt = conn.prepareStatement(selectQuery)) {
            for (Configuration configuration : ibisManager.getConfigurations()) {
                String configName = configuration.getName();
                configNames.add(configName);
                if (DATABASE_CLASSLOADER.equals(configuration.getClassLoaderType())) {
                    stmt.setString(1, configName);
                    try (ResultSet rs = stmt.executeQuery()) {
                        if (rs.next()) {
                            String ibisConfigVersion = rs.getString(1);
                            // DatabaseClassLoader configurations always have a version
                            String configVersion = configuration.getVersion();
                            if (StringUtils.isEmpty(configVersion) && configuration.getClassLoader() != null) {
                                // If config hasn't loaded yet, don't skip it!
                                log.warn(getLogPrefix() + "skipping autoreload for configuration [" + configName + "] unable to determine [configuration.version]");
                            } else if (!StringUtils.equalsIgnoreCase(ibisConfigVersion, configVersion)) {
                                log.info(getLogPrefix() + "configuration [" + configName + "] with version [" + configVersion + "] will be reloaded with new version [" + ibisConfigVersion + "]");
                                configsToReload.add(configName);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        getMessageKeeper().add("error while executing query [" + selectQuery + "] (as part of scheduled job execution)", e);
    } finally {
        qs.close();
    }
    if (!configsToReload.isEmpty()) {
        for (String configToReload : configsToReload) {
            ibisManager.getIbisContext().reload(configToReload);
        }
    }
    if (CONFIG_AUTO_DB_CLASSLOADER) {
        // load new (activated) configs
        List<String> dbConfigNames = null;
        try {
            dbConfigNames = ConfigurationUtils.retrieveConfigNamesFromDatabase(ibisManager.getIbisContext());
        } catch (ConfigurationException e) {
            getMessageKeeper().add("error while retrieving configuration names from database", e);
        }
        if (dbConfigNames != null && !dbConfigNames.isEmpty()) {
            for (String currentDbConfigurationName : dbConfigNames) {
                if (!configNames.contains(currentDbConfigurationName)) {
                    ibisManager.getIbisContext().load(currentDbConfigurationName);
                }
            }
            // unload old (deactivated) configurations
            for (String currentConfigurationName : configNames) {
                if (!dbConfigNames.contains(currentConfigurationName) && DATABASE_CLASSLOADER.equals(ibisManager.getConfiguration(currentConfigurationName).getClassLoaderType())) {
                    ibisManager.getIbisContext().unload(currentConfigurationName);
                }
            }
        }
    }
}
Also used : Configuration(nl.nn.adapterframework.configuration.Configuration) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Aggregations

FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)32 Connection (java.sql.Connection)17 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SenderException (nl.nn.adapterframework.core.SenderException)13 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)11 Parameter (nl.nn.adapterframework.parameters.Parameter)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 SQLException (java.sql.SQLException)8 LinkedHashMap (java.util.LinkedHashMap)8 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)8 Map (java.util.Map)7 PipeRunException (nl.nn.adapterframework.core.PipeRunException)7 JdbcException (nl.nn.adapterframework.jdbc.JdbcException)7 ArrayList (java.util.ArrayList)6 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 ListenerException (nl.nn.adapterframework.core.ListenerException)4 IbisWebServiceSender (nl.nn.adapterframework.http.IbisWebServiceSender)4 WebServiceListener (nl.nn.adapterframework.http.WebServiceListener)4