Examples of ServiceRegistration


Examples of org.osgi.framework.ServiceRegistration

     * The job is executed once and finished successfully.
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobWithIdExecution() throws Exception {
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        return JobResult.OK;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, "myid1", null);
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     * The job execution always fails
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testForceCancelJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        sleep(1000);
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, "myid3", null);
            cb.block();

            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
            // job is currently sleeping, but force cancel always waits!
            final Event e = jobManager.findJob("sling/test", Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid3"));
            assertNotNull(e);
            jobManager.forceRemoveJob((String)e.getProperty(ResourceHelper.PROPERTY_JOB_ID));
            // the job is now removed
            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
            try {
                assertEquals(1, col.size());
            } finally {
                for(final Job j : col) {
                    jobManager.removeJobById(j.getId());
                }
            }
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     */
    protected ServiceRegistration registerEventHandler(final String topic,
            final EventHandler handler) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(EventConstants.EVENT_TOPIC, topic);
        final ServiceRegistration reg = this.bc.registerService(EventHandler.class.getName(),
                handler, props);
        return reg;
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     */
    protected ServiceRegistration registerJobConsumer(final String topic,
            final JobConsumer handler) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(JobConsumer.PROPERTY_TOPICS, topic);
        final ServiceRegistration reg = this.bc.registerService(JobConsumer.class.getName(),
                handler, props);
        return reg;
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     */
    protected ServiceRegistration registerJobExecutor(final String topic,
            final JobExecutor handler) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(JobConsumer.PROPERTY_TOPICS, topic);
        final ServiceRegistration reg = this.bc.registerService(JobExecutor.class.getName(),
                handler, props);
        return reg;
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(HealthCheck.NAME, "name_" + id);
        props.put(HealthCheck.TAGS, id);
        props.put(HealthCheck.ASYNC_CRON_EXPRESSION, "*/1 * * * * ?");
       
        @SuppressWarnings("rawtypes")
        final ServiceRegistration reg = bundleContext.registerService(HealthCheck.class, hc, props);
       
        try {
            {
                // Wait for HC to be registered
                final long timeout = System.currentTimeMillis() + 10000L;
                boolean hcFound = false;
                while(System.currentTimeMillis() < timeout) {
                    final List<HealthCheckExecutionResult> results = executor.execute(id);
                    if(!results.isEmpty()) {
                        hcFound = true;
                        break;
                    }
                    Thread.sleep(100L);
                }
                assertTrue("Expecting HC to become active", hcFound);
            }
           
            // Now reset the counter and check that HC increments it even if we don't
            // use the executor
            {
                counter.set(0);
                final long timeout = System.currentTimeMillis() + 5000L;
                while(System.currentTimeMillis() < timeout) {
                    if(counter.get() > 0) {
                        break;
                    }
                    Thread.sleep(100L);
                }
                assertTrue("Expecting counter to be incremented", counter.get() > 0);
            }
           
            // Verify that we get the right log
            final String msg = executor.execute(id).get(0).getHealthCheckResult().iterator().next().getMessage();
            assertTrue("Expecting the right message: " + msg, msg.contains("counter is now"));
           
            // And verify that calling executor lots of times doesn't increment as much
            final int previous = counter.get();
            final int n = 100;
            for(int i=0; i < n; i++) {
                executor.execute(id);
            }
            assertTrue("Expecting counter to increment asynchronously", counter.get() < previous + n);
        } finally {
            reg.unregister();
        }
       
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     * can use this to manage repository lifecycle. Default implementation registers the BeanFactory
     * instance which would be used by the Repository creator to create the repository
     */
    protected void dependenciesSatisfied() {
        //TODO Review the thread safety aspect
        ServiceRegistration reg = beanFactoryReg;
        if (reg == null) {
            beanFactoryReg = bundleContext.registerService(BeanFactory.class.getName(), this, new Properties());
            log.info("All dependencies are satisfied. Registering the BeanFactory instance");
        }
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     * can use this to manage repository lifecycle. Default implementation de-registers the BeanFactory
     * instance. And repository creator service which then depends on BeanFactory reference would then be notified and
     * can react accordingly
     */
    protected void dependenciesUnSatisfied() {
        ServiceRegistration reg = beanFactoryReg;
        if (reg != null) {
            reg.unregister();
            beanFactoryReg = null;
            log.info("Dependencies unsatisfied. Deregistering the BeanFactory instance");
        }
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

    private void unregisterStatistics(Repository repository) {
        if (repository instanceof RepositoryImpl) {
            String mbeanName = StatisticsMBeanImpl.getMBeanName((RepositoryImpl) repository);
            try {
                ServiceRegistration serviceRegistration = statisticsServices.get(mbeanName);
                if (serviceRegistration != null) {
                    serviceRegistration.unregister();
                }
            } catch (Exception e) {
                log.warn("Failed to unregister statistics JMX bean {} ", e.getMessage());
            }
            statisticsServices.remove(mbeanName);
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

    }

    @Test
    public void testOsgiResourceEvents() throws RepositoryException {
        final ResourceEventListener listener = new ResourceEventListener();
        final ServiceRegistration reg = listener.register(bundleContext, SlingConstants.TOPIC_RESOURCE_ADDED);
        final Session s = repository.loginAdministrative(null);
        final int nPaths = 2500 * TEST_SCALE;
        final int timeoutMsec = 2 * nPaths;
        final String prefix = uniqueName("testOsgiResourceEvents");

        // Create N nodes with a unique name under /
        // and verify that ResourceEventListener gets an event
        // for each of them
        try {
            for(int i=0; i  < nPaths; i++) {
                s.getRootNode().addNode(prefix + i);
            }
            s.save();

            log.info("Added {} nodes, checking what ResourceEventListener got...", nPaths);
            final long timeout = System.currentTimeMillis() + timeoutMsec;
            final Set<String> missing = new HashSet<String>();
            while(System.currentTimeMillis() < timeout) {
                missing.clear();
                final Set<String> paths = listener.getPaths();
                for(int i=0; i  < nPaths; i++) {
                    final String path = "/" + prefix + i;
                    if(!paths.contains(path)) {
                        missing.add(path);
                    }
                }

                if(missing.isEmpty()) {
                    break;
                }
            }

            if(!missing.isEmpty()) {
                final String missingStr = missing.size() > 10 ? missing.size() + " paths missing" : missing.toString();
                fail("OSGi add resource events are missing for "
                        + missing.size() + "/" + nPaths + " paths after "
                        + timeoutMsec + " msec: " + missingStr);
            }
        } finally {
            reg.unregister();
            s.logout();
        }
       
        log.info("Successfuly detected OSGi observation events for " + nPaths + " paths");
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.