Package org.osgi.framework

Examples of org.osgi.framework.ServiceRegistration


        commandProcessorTracker.close();
       
        Iterator<ServiceRegistration> iterator = regs.iterator();
        while (iterator.hasNext())
        {
            ServiceRegistration reg = iterator.next();
            reg.unregister();
            iterator.remove();
        }
    }
View Full Code Here


    @Test
    public void test_async_listener() throws IOException
    {
        final String pid = "test_listener";
        final TestListener testListener = new TestListener();
        final ServiceRegistration listener = this.bundleContext.registerService( ConfigurationListener.class.getName(),
            testListener, null );
        int eventCount = 0;

        Configuration config = configure( pid, null, false );
        try
        {
            delay();
            testListener.assertNoEvent();

            config.update( new Hashtable<String, Object>()
            {
                {
                    put( "x", "x" );
                }
            } );
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCount );

            config.update( new Hashtable<String, Object>()
            {
                {
                    put( "x", "x" );
                }
            } );
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCount );

            config.setBundleLocation( "new_Location" );
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCount );

            config.update();
            testListener.assertNoEvent();

            config.delete();
            config = null;
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCount );
        }
        finally
        {
            if ( config != null )
            {
                try
                {
                    config.delete();
                }
                catch ( IOException ioe )
                {
                    // ignore
                }
            }

            listener.unregister();
        }
    }
View Full Code Here

        Configuration config = configure( pid, null, false );

        // Synchronous listener expecting synchronous events being
        // registered as a SynchronousConfigurationListener
        final TestListener testListener = new SynchronousTestListener();
        final ServiceRegistration listener = this.bundleContext.registerService(
            SynchronousConfigurationListener.class.getName(), testListener, null );

        // Synchronous listener expecting asynchronous events being
        // registered as a regular ConfigurationListener
        final TestListener testListenerAsync = new SynchronousTestListener();
        final ServiceRegistration listenerAsync = this.bundleContext.registerService(
            ConfigurationListener.class.getName(), testListenerAsync, null );

        int eventCount = 0;
        int eventCountAsync = 0;

        try
        {
            delay();
            testListener.assertNoEvent();
            testListenerAsync.assertNoEvent();

            config.update( new Hashtable<String, Object>()
            {
                {
                    put( "x", "x" );
                }
            } );
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount );
            testListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync );

            config.update( new Hashtable<String, Object>()
            {
                {
                    put( "x", "x" );
                }
            } );
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount );
            testListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync );

            config.setBundleLocation( "new_Location" );
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, false, ++eventCount );
            testListenerAsync.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCountAsync );

            config.update();
            testListener.assertNoEvent();
            testListenerAsync.assertNoEvent();

            config.delete();
            config = null;
            delay();
            testListener.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, false, ++eventCount );
            testListenerAsync.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCountAsync );
        }
        finally
        {
            if ( config != null )
            {
                try
                {
                    config.delete();
                }
                catch ( IOException ioe )
                {
                    // ignore
                }
            }

            listener.unregister();
            listenerAsync.unregister();
        }
    }
View Full Code Here

     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobExecutionUsingJobConsumer() throws Exception {
        final Barrier cb = new Barrier(2);

        final ServiceRegistration reg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

            @Override
                    public JobResult process(final Job job) {
                        cb.block();
                        return JobResult.OK;
                    }
                 });

        try {
            this.getJobManager().addJob(TOPIC, 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 {
            reg.unregister();
        }
    }
View Full Code Here

     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobExecutionUsingJobExecutor() throws Exception {
        final Barrier cb = new Barrier(2);

        final ServiceRegistration reg = this.registerJobExecutor(TOPIC,
                new JobExecutor() {

                    @Override
                    public JobExecutionResult process(final Job job, final JobExecutionContext context) {
                        cb.block();
                        return context.result().succeeded();
                    }
                });

        try {
            this.getJobManager().addJob(TOPIC, 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 {
            reg.unregister();
        }
    }
View Full Code Here

        }
    }

    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testManyJobs() throws Exception {
        final ServiceRegistration reg1 = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(final Job job) {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException ie) {
                            // ignore
                        }
                        return JobResult.OK;
                    }

                 });
        final AtomicInteger count = new AtomicInteger(0);
        final ServiceRegistration reg2 = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED,
                new EventHandler() {
                    @Override
                    public void handleEvent(Event event) {
                        count.incrementAndGet();
                    }
                 });

        try {
            // we start "some" jobs
            final int COUNT = 300;
            for(int i = 0; i < COUNT; i++ ) {
                this.getJobManager().addJob(TOPIC, null);
            }
            while ( count.get() < COUNT ) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                    // ignore
                }
            }
            assertEquals("Finished count", COUNT, count.get());
            assertEquals("Finished count", COUNT, this.getJobManager().getStatistics().getNumberOfFinishedJobs());
        } finally {
            reg1.unregister();
            reg2.unregister();
        }
    }
View Full Code Here

     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testCancelJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final Barrier cb2 = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        cb2.block();
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
            cb.block();

            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[])null).size());
            // job is currently waiting, therefore cancel fails
            final Job e1 = jobManager.getJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
            assertNotNull(e1);
            cb2.block(); // and continue job

            sleep(200);

            // the job is now in the queue again
            final Job e2 = jobManager.getJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
            assertNotNull(e2);
            assertTrue(jobManager.removeJobById(e2.getId()));
            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[])null).size());
            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, TOPIC, -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

     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testGetJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final Barrier cb2 = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        cb2.block();
                        return JobResult.OK;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            final Job j = jobManager.addJob(TOPIC, null);
            cb.block();

            assertNotNull(jobManager.getJob(TOPIC, null));

            cb2.block(); // and continue job

            jobManager.removeJobById(j.getId());
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here

     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testStartJobAndReschedule() throws Exception {
        final List<Integer> retryCountList = new ArrayList<Integer>();
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {
                    int retryCount;

                    @Override
                    public JobResult process(Job job) {
                        int retry = 0;
                        if ( job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT) != null ) {
                            retry = (Integer)job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
                        }
                        if ( retry == retryCount ) {
                            retryCountList.add(retry);
                        }
                        retryCount++;
                        cb.block();
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            final Job job = jobManager.addJob(TOPIC, null);

            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            // the job is retried after two seconds, so we wait again
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            // the job is retried after two seconds, so we wait again
            assertTrue("No event received in the given time.", cb.block(5));
            // we have reached the retry so we expect to not get an event
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
            assertEquals("Unexpected number of retries", 3, retryCountList.size());

            jobManager.removeJobById(job.getId());
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here

    public void testNotifications() throws Exception {
        final List<String> cancelled = Collections.synchronizedList(new ArrayList<String>());
        final List<String> failed = Collections.synchronizedList(new ArrayList<String>());
        final List<String> finished = Collections.synchronizedList(new ArrayList<String>());
        final List<String> started = Collections.synchronizedList(new ArrayList<String>());
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        // events 1 and 4 finish the first time
                        final String id = (String)job.getProperty("id");
                        if ( "1".equals(id) || "4".equals(id) ) {
                            return JobResult.OK;

                        // 5 fails always
                        } else if ( "5".equals(id) ) {
                            return JobResult.FAILED;
                        } else {
                            int retry = 0;
                            if ( job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT) != null ) {
                                retry = (Integer)job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT);
                            }
                            // 2 fails the first time
                            if ( "2".equals(id) ) {
                                if ( retry == 0 ) {
                                    return JobResult.FAILED;
                                } else {
                                    return JobResult.OK;
                                }
                            }
                            // 3 fails the first and second time
                            if ( "3".equals(id) ) {
                                if ( retry == 0 || retry == 1 ) {
                                    return JobResult.FAILED;
                                } else {
                                    return JobResult.OK;
                                }
                            }
                        }
                        return JobResult.FAILED;
                    }
                });
        final ServiceRegistration eh1Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_CANCELLED,
                new EventHandler() {

                    @Override
                    public void handleEvent(Event event) {
                        final String id = (String)event.getProperty("id");
                        cancelled.add(id);
                    }
                });
        final ServiceRegistration eh2Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FAILED,
                new EventHandler() {

                    @Override
                    public void handleEvent(Event event) {
                        final String id = (String)event.getProperty("id");
                        failed.add(id);
                    }
                });
        final ServiceRegistration eh3Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED,
                new EventHandler() {

                    @Override
                    public void handleEvent(Event event) {
                        final String id = (String)event.getProperty("id");
                        finished.add(id);
                    }
                });
        final ServiceRegistration eh4Reg = this.registerEventHandler(NotificationConstants.TOPIC_JOB_STARTED,
                new EventHandler() {

                    @Override
                    public void handleEvent(Event event) {
                        final String id = (String)event.getProperty("id");
                        started.add(id);
                    }
                });

        final JobManager jobManager = this.getJobManager();
        try {
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"1"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"2"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"3"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"4"));
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"5"));

            int count = 0;
            final long startTime = System.currentTimeMillis();
            do {
                count = finished.size() + cancelled.size();
                // after 25 seconds we cancel the test
                if ( System.currentTimeMillis() - startTime > 25000 ) {
                    throw new Exception("Timeout during notification test.");
                }
            } while ( count < 5 || started.size() < 10 );
            assertEquals("Finished count", 4, finished.size());
            assertEquals("Cancelled count", 1, cancelled.size());
            assertEquals("Started count", 10, started.size());
            assertEquals("Failed count", 5, failed.size());
        } finally {
            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
            for(final Job j : col) {
                jobManager.removeJobById(j.getId());
            }
            jcReg.unregister();
            eh1Reg.unregister();
            eh2Reg.unregister();
            eh3Reg.unregister();
            eh4Reg.unregister();
        }
    }
View Full Code Here

TOP

Related Classes of org.osgi.framework.ServiceRegistration

Copyright © 2018 www.massapicom. 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.