Package org.apache.openejb.util

Examples of org.apache.openejb.util.Duration


    }

    public void deploy(BeanContext beanContext) throws OpenEJBException {
        Options options = new Options(beanContext.getProperties());

        Duration accessTimeout = getDuration(options, "Timeout", this.accessTimeout, TimeUnit.MILLISECONDS);
        accessTimeout = getDuration(options, "AccessTimeout", accessTimeout, TimeUnit.MILLISECONDS);
        Duration closeTimeout = getDuration(options, "CloseTimeout", this.closeTimeout, TimeUnit.MINUTES);

        final ObjectRecipe recipe = PassthroughFactory.recipe(new Pool.Builder(poolBuilder));
        recipe.allow(Option.CASE_INSENSITIVE_FACTORY);
        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
View Full Code Here


        if (duration.getUnit() == null) duration.setUnit(unit);
    }

    private Duration getDuration(Options options, String property, Duration defaultValue, TimeUnit defaultUnit) {
        String s = options.get(property, defaultValue.toString());
        Duration duration = new Duration(s);
        if (duration.getUnit() == null) duration.setUnit(defaultUnit);
        return duration;
    }
View Full Code Here

    protected final Cache<Object, Instance> cache;
    private final ConcurrentHashMap<Object, Instance> checkedOutInstances = new ConcurrentHashMap<Object, Instance>();
    private final SessionContext sessionContext;

    public StatefulContainer(Object id, SecurityService securityService, Cache<Object, Instance> cache) {
        this(id, securityService, cache, new Duration(-1, TimeUnit.MILLISECONDS));
    }
View Full Code Here

                // remember instance until it is returned to the cache               
                checkedOutInstances.put(primaryKey, instance);
            }
        }
       
        Duration accessTimeout = getAccessTimeout(instance.beanContext, callMethod);

        final Lock currLock = instance.getLock();
        final boolean lockAcquired;
        if (accessTimeout == null || accessTimeout.getTime() < 0) {
            // wait indefinitely for a lock
            currLock.lock();
            lockAcquired = true;
        } else if (accessTimeout.getTime() == 0) {
            // concurrent calls are not allowed, lock only once
        lockAcquired = currLock.tryLock();
      } else {
        // try to get a lock within the specified period.
        try {
        lockAcquired = currLock.tryLock(accessTimeout.getTime(), accessTimeout.getUnit());
      } catch (InterruptedException e) {
        throw new ApplicationException("Unable to get lock.", e);
      }
      }
        // Did we acquire the lock to the current execution?
View Full Code Here

    }

    private Duration getAccessTimeout(BeanContext beanContext, Method callMethod) {
        callMethod = beanContext.getMatchingBeanMethod(callMethod);
       
        Duration accessTimeout = beanContext.getAccessTimeout(callMethod);
        if (accessTimeout == null) {
            accessTimeout = beanContext.getAccessTimeout();
            if (accessTimeout == null) {
                accessTimeout = this.accessTimeout;
            }
View Full Code Here

    }

    protected Object _invoke(Method callMethod, Method runMethod, Object[] args, Instance instance, ThreadContext callContext, InterfaceType callType) throws OpenEJBException {
        BeanContext beanContext = callContext.getBeanContext();
              
        Duration accessTimeout = getAccessTimeout(beanContext, runMethod);
        boolean read = beanContext.getConcurrencyAttribute(runMethod) == javax.ejb.LockType.READ;
       
        final Lock lock = aquireLock(read, accessTimeout, instance, runMethod);

        Object returnValue;
View Full Code Here

        return returnValue;
    }

    private Duration getAccessTimeout(BeanContext beanContext, Method callMethod) {
        Duration accessTimeout = beanContext.getAccessTimeout(callMethod);
        if (accessTimeout == null) {
            accessTimeout = beanContext.getAccessTimeout();
            if (accessTimeout == null) {
                accessTimeout = this.accessTimeout;
            }
View Full Code Here

        // ejbTimeout
        deployment.setEjbTimeout(getTimeout(ejbClass, bean.timeoutMethod));

        if (bean.statefulTimeout != null) {
            deployment.setStatefulTimeout(new Duration(bean.statefulTimeout.time, TimeUnit.valueOf(bean.statefulTimeout.unit)));
        }

        if (bean instanceof StatefulBeanInfo) {
            StatefulBeanInfo statefulBeanInfo = (StatefulBeanInfo) bean;
View Full Code Here

    public void test() throws Exception {

        // To run in an IDE, uncomment and update this line
        System.setProperty("version", OpenEjbVersion.get().getVersion());

        final Duration reconnectDelay = new Duration("1 second");

        final File zip = Repository.getArtifact("org.apache.openejb", "openejb-standalone", "zip");
        final File app = Repository.getArtifact("org.apache.openejb.itests", "failover-ejb", "jar");

        final File dir = Files.tmpdir();

        System.setProperty("openejb.client.requestretry", "true");

        final Map<String, StandaloneServer> servers = new HashMap<String, StandaloneServer>();

        for (final String name : new String[]{"red", "green", "blue"}) {
            final File home = new File(dir, name);
            Files.mkdir(home);
            Zips.unzip(zip, home, true);

            final StandaloneServer server = new StandaloneServer(home, home);
            server.killOnExit();
            server.ignoreOut();
            server.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb");
            server.getProperties().put("name", name);
            server.getProperties().put("openejb.extract.configuration", "false");

            IO.copy(app, Files.path(home, "apps", "itest.jar"));
            IO.copy(IO.read("<openejb><Deployments dir=\"apps/\"/></openejb>"), Files.path(home, "conf", "openejb.xml"));

            final StandaloneServer.ServerService ejbd = server.getServerService("ejbd");
            ejbd.setDisabled(false);
            ejbd.setBind("0.0.0.0");
            ejbd.setPort(getAvailablePort());
            ejbd.setThreads(5);
            ejbd.set("discoveryHost", "localhost");
            ejbd.set("discovery", "ejb:ejbd://{discoveryHost}:{port}/" + name);

            final StandaloneServer.ServerService multipoint = server.getServerService(MULTIPOINT);
            multipoint.setBind("0.0.0.0");
            multipoint.setPort(getAvailablePort());
            multipoint.setDisabled(false);
            multipoint.set("discoveryHost", "localhost");
            multipoint.set("discoveryName", name);
            multipoint.set("reconnectDelay", reconnectDelay.toString());

            servers.put(name, server);
        }

        final StandaloneServer red = servers.get("red");

        // Set all the initialServers to point to RED
        for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
            final StandaloneServer server = entry.getValue();
            final StandaloneServer.ServerService multipoint = server.getServerService(MULTIPOINT);
            multipoint.set("initialServers", "localhost:" + red.getServerService(MULTIPOINT).getPort());
        }

        // Start all the servers except RED
        for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
            if (entry.getKey().equals("red")) continue;
            entry.getValue().start(1, TimeUnit.MINUTES);
        }

        // Verify Failover is not yet functional

        {
            // RED was never started so BLUE never found any peers

            // Lets invoke BLUE then shut it down and verify we have
            // no other peers to invoke
            final StandaloneServer blue = servers.get("blue");
            final Properties environment = new Properties();
            environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
            environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + blue.getServerService("ejbd").getPort());

            final InitialContext context = new InitialContext(environment);
            final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");

            // Invoke BLUE a few times
            invoke(bean, 10, "blue");

            // Kill BLUE
            blue.kill();

            // Invocations should now fail (and not failover)
            try {
                bean.name();
                Assert.fail("Server should be down and failover not hooked up");
            } catch (final Exception e) {
                // pass
            }
        }

        // Now we start RED
        red.start(1, TimeUnit.MINUTES);

        // Wait for the reconnectDelay so GREEN can find RED
        Thread.sleep((long) (reconnectDelay.getTime(TimeUnit.MILLISECONDS) * 1.5));

        // Verify Failover is now functional

        {
            // RED was never started so GREEN never found any peers
View Full Code Here

    public void test() throws Exception {

        // To run in an IDE, uncomment and update this line
        System.setProperty("version", OpenEjbVersion.get().getVersion());

        final Duration reconnectDelay = new Duration("1 second");

        final File zip = Repository.getArtifact("org.apache.openejb", "openejb-standalone", "zip");
        final File app = Repository.getArtifact("org.apache.openejb.itests", "failover-ejb", "jar");

        final File dir = Files.tmpdir();

        System.setProperty("openejb.client.requestretry", "true");

        final Map<String, StandaloneServer> servers = new HashMap<String, StandaloneServer>();

        for (final String name : new String[]{"red", "green", "blue"}) {
            final File home = new File(dir, name);
            Files.mkdir(home);
            Zips.unzip(zip, home, true);

            final StandaloneServer server = new StandaloneServer(home, home);
            server.killOnExit();
            server.ignoreOut();
            server.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb");
            server.getProperties().put("name", name);
            server.getProperties().put("openejb.extract.configuration", "false");

            IO.copy(app, Files.path(home, "apps", "itest.jar"));
            IO.copy(IO.read("<openejb><Deployments dir=\"apps/\"/></openejb>"), Files.path(home, "conf", "openejb.xml"));

            final StandaloneServer.ServerService ejbd = server.getServerService("ejbd");
            ejbd.setDisabled(false);
            ejbd.setBind("0.0.0.0");
            ejbd.setPort(getAvailablePort());
            ejbd.setThreads(5);
            ejbd.set("discoveryHost", "localhost");
            ejbd.set("discovery", "ejb:ejbd://{discoveryHost}:{port}/" + name);

            final StandaloneServer.ServerService multipoint = server.getServerService(MULTIPOINT);
            multipoint.setBind("0.0.0.0");
            multipoint.setPort(getAvailablePort());
            multipoint.setDisabled(false);
            multipoint.set("discoveryHost", "localhost");
            multipoint.set("discoveryName", name);
            multipoint.set("reconnectDelay", reconnectDelay.toString());

            servers.put(name, server);
        }

        final StandaloneServer red = servers.get("red");

        // Set all the initialServers to point to RED
        for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
            final StandaloneServer server = entry.getValue();
            final StandaloneServer.ServerService multipoint = server.getServerService(MULTIPOINT);
            final String value = "localhost:" + red.getServerService(MULTIPOINT).getPort() + ",localhost:"+multipoint.getPort();
            multipoint.set("initialServers", value);
        }

        // Start all the servers except RED
        for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
            if (entry.getKey().equals("red")) continue;
            entry.getValue().start(1, TimeUnit.MINUTES);
        }

        // Verify Failover is not yet functional

        {
            // RED was never started so BLUE never found any peers

            // Lets invoke BLUE then shut it down and verify we have
            // no other peers to invoke
            final StandaloneServer blue = servers.get("blue");
            final Properties environment = new Properties();
            environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
            environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + blue.getServerService("ejbd").getPort());

            final InitialContext context = new InitialContext(environment);
            final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");

            // Invoke BLUE a few times
            invoke(bean, 10, "blue");

            // Kill BLUE
            blue.kill();

            // Invocations should now fail (and not failover)
            try {
                bean.name();
                Assert.fail("Server should be down and failover not hooked up");
            } catch (final Exception e) {
                // pass
            }
        }

        // Now we start RED
        red.start(1, TimeUnit.MINUTES);

        // Wait for the reconnectDelay so GREEN can find RED
        Thread.sleep((long) (reconnectDelay.getTime(TimeUnit.MILLISECONDS) * 1.5));

        // Verify Failover is now functional

        {
            // RED was never started so GREEN never found any peers
View Full Code Here

TOP

Related Classes of org.apache.openejb.util.Duration

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.