Search in sources :

Example 1 with EventDistributor

use of org.teiid.events.EventDistributor in project teiid by teiid.

the class AbstractEventDistributorFactoryService method start.

public void start() {
    final EventDistributor ed = new EventDistributorImpl() {

        @Override
        public VDBRepository getVdbRepository() {
            return AbstractEventDistributorFactoryService.this.getVdbRepository();
        }

        @Override
        public DQPCore getDQPCore() {
            return AbstractEventDistributorFactoryService.this.getDQPCore();
        }
    };
    ObjectReplicator objectReplicator = getObjectReplicator();
    // this instance is by use of teiid internally; only invokes the remote instances
    if (objectReplicator != null) {
        try {
            // $NON-NLS-1$
            this.replicatableEventDistributor = objectReplicator.replicate("$TEIID_ED$", EventDistributor.class, ed, 0);
        } catch (Exception e) {
            LogManager.logError(LogConstants.CTX_RUNTIME, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40088, this));
        }
    }
    // for external client to call. invokes local instance and remote ones too.
    this.eventDistributorProxy = (EventDistributor) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { EventDistributor.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Replicated annotation = method.getAnnotation(Replicated.class);
            Object result = null;
            try {
                if (replicatableEventDistributor == null || (annotation != null && annotation.remoteOnly())) {
                    result = method.invoke(ed, args);
                }
                if (replicatableEventDistributor != null) {
                    result = method.invoke(replicatableEventDistributor, args);
                }
            } catch (InvocationTargetException e) {
                throw e.getTargetException();
            }
            return result;
        }
    });
}
Also used : EventDistributorImpl(org.teiid.deployers.EventDistributorImpl) Replicated(org.teiid.Replicated) EventDistributor(org.teiid.events.EventDistributor) ObjectReplicator(org.teiid.query.ObjectReplicator) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with EventDistributor

use of org.teiid.events.EventDistributor in project teiid by teiid.

the class TestExternalMatViews method testLazyUpdate.

@Test
public void testLazyUpdate() throws Exception {
    HardCodedExecutionFactory hcef = setupData(server);
    ModelMetaData sourceModel = setupSourceModel();
    ModelMetaData matViewModel = setupMatViewModel();
    ModelMetaData viewModel = new ModelMetaData();
    viewModel.setName("view1");
    viewModel.setModelType(Type.VIRTUAL);
    viewModel.addSourceMetadata("DDL", "CREATE VIEW v1 (col integer primary key, col1 string) " + "OPTIONS (MATERIALIZED true, " + "MATERIALIZED_TABLE 'matview.MAT_V2', " + "\"teiid_rel:ALLOW_MATVIEW_MANAGEMENT\" true, " + "\"teiid_rel:MATVIEW_STATUS_TABLE\" 'matview.STATUS', " + "\"teiid_rel:MATVIEW_MAX_STALENESS_PCT\" '10', " + "\"teiid_rel:MATVIEW_LOADNUMBER_COLUMN\" 'loadnum') " + "AS select col, col1 from source.physicalTbl");
    server.deployVDB("comp", sourceModel, viewModel, matViewModel);
    Thread.sleep(1000);
    // check if materilization is loaded
    Connection c = h2DataSource.getConnection();
    ResultSet rs = c.createStatement().executeQuery("SELECT LoadState, StaleCount FROM Status WHERE VDBName = 'comp'");
    rs.next();
    assertEquals("LOADED", rs.getString(1));
    assertEquals(0, rs.getInt(2));
    // verify with querying the database.
    conn = server.createConnection("jdbc:teiid:comp");
    Statement s = conn.createStatement();
    rs = s.executeQuery("select * from view1.v1 order by col");
    rs.next();
    assertEquals(1, rs.getInt(1));
    assertEquals("town", rs.getString(2));
    // send a row update
    EventDistributor ed = server.getEventDistributor();
    ResultsFuture<?> f = ed.dataModification("comp", "1", "source", "physicalTbl", new Object[] { 1, "town" }, new Object[] { 1, "town-modified" }, new String[] { "col1", "col2" });
    f.get();
    f = ed.dataModification("comp", "1", "source", "physicalTbl", new Object[] { 1, "town" }, new Object[] { 1, "town-modified" }, new String[] { "col1", "col2" });
    f.get();
    // check the stale count incremented
    rs = c.createStatement().executeQuery("SELECT LoadState, StaleCount FROM Status WHERE VDBName = 'comp'");
    rs.next();
    assertEquals("NEEDS_LOADING", rs.getString(1));
    assertEquals(1, rs.getInt(2));
    // now reload the view, this should reset the stalecount. If you can wait one minute this occurs automatically
    conn.createStatement().execute("exec SYSADMIN.loadMatView(schemaName=>'view1', viewName=>'v1', invalidate=>false)");
    // check the stale count to zero
    rs = c.createStatement().executeQuery("SELECT LoadState, StaleCount FROM Status WHERE VDBName = 'comp'");
    rs.next();
    assertEquals("LOADED", rs.getString(1));
    assertEquals(0, rs.getInt(2));
    String ddl = server.getAdmin().getSchema("comp", "1", "source", null, null);
    assertFalse(ddl.contains("AFTER INSERT"));
}
Also used : Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) EventDistributor(org.teiid.events.EventDistributor) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Aggregations

EventDistributor (org.teiid.events.EventDistributor)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Test (org.junit.Test)1 Replicated (org.teiid.Replicated)1 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)1 EventDistributorImpl (org.teiid.deployers.EventDistributorImpl)1 ObjectReplicator (org.teiid.query.ObjectReplicator)1 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)1