Search in sources :

Example 36 with EJBException

use of javax.ejb.EJBException in project tomee by apache.

the class RoundRobinConnectionStrategyTest method test.

@Test
public void test() throws Exception {
    // To run in an IDE, uncomment and update this line
    // System.setProperty("version", OpenEjbVersion.get().getVersion());
    System.setProperty("openejb.client.connection.strategy", "roundrobin");
    final File zip = Repository.getArtifact("org.apache.tomee", "openejb-standalone", "zip");
    final File app = Repository.getArtifact("org.apache.openejb.itests", "failover-ejb", "jar");
    final File dir = Files.tmpdir();
    final StandaloneServer root;
    {
        final String name = "root";
        final File home = new File(dir, name);
        Files.mkdir(home);
        Zips.unzip(zip, home, true);
        root = new StandaloneServer(home, home);
        root.killOnExit();
        // root.setDebug(Boolean.getBoolean("openejb.test.standalone." + name + ".debug"));
        root.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb.itest");
        root.ignoreOut();
        root.setProperty("name", name);
        root.setProperty("openejb.extract.configuration", "false");
        final StandaloneServer.ServerService multipoint = root.getServerService("multipoint");
        multipoint.setBind("localhost");
        multipoint.setPort(getNextAvailablePort());
        multipoint.setDisabled(false);
        multipoint.set("discoveryName", name);
        logger.info("Starting Root server");
        root.start();
    }
    final Services services = new Services();
    Client.addEventObserver(services);
    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.setDebug(Boolean.getBoolean("openejb.test.standalone." + name + ".debug"));
        server.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb.itest");
        server.setProperty("name", name);
        server.setProperty("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.setBind("localhost");
        ejbd.setDisabled(false);
        ejbd.setPort(getNextAvailablePort());
        ejbd.setThreads(5);
        final URI uri = URI.create(String.format("ejbd://%s:%s/%s", ejbd.getBind(), ejbd.getPort(), name));
        ejbd.set("discovery", "ejb:" + uri);
        services.add(uri);
        server.getContext().set(URI.class, uri);
        final StandaloneServer.ServerService multipoint = server.getServerService("multipoint");
        multipoint.setPort(getNextAvailablePort());
        multipoint.setDisabled(false);
        multipoint.set("discoveryName", name);
        multipoint.set("initialServers", "localhost:" + root.getServerService("multipoint").getPort());
        servers.put(name, server);
        logger.info(String.format("Starting %s server, uri=%s", name, uri));
        server.start(1, TimeUnit.MINUTES);
    }
    System.setProperty("openejb.client.requestretry", "true");
    System.setProperty("openejb.client.connection.strategy", "roundrobin");
    logger.info("Beginning Test");
    final Properties environment = new Properties();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
    environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + servers.values().iterator().next().getServerService("ejbd").getPort() + "/provider");
    final InitialContext context = new InitialContext(environment);
    final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");
    for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
        final String name = entry.getKey();
        final StandaloneServer server = entry.getValue();
        final URI serverURI = server.getContext().get(URI.class);
        logger.info("Waiting for updated list");
        services.assertServices(10, TimeUnit.SECONDS, new CalculatorCallable(bean), 500);
        logger.info("Asserting balance");
        assertBalance(bean, services.get().size());
        logger.info("Shutting down " + name);
        server.kill();
        services.remove(serverURI);
    }
    logger.info("All Servers Shutdown");
    try {
        logger.info("Making one last request, expecting complete failover");
        final String name = bean.name();
        Assert.fail("Server should be destroyed: " + name);
    } catch (final EJBException e) {
        logger.info(String.format("Pass.  Request resulted in %s: %s", e.getCause().getClass().getSimpleName(), e.getMessage()));
    // good
    }
    for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
        final String name = entry.getKey();
        final StandaloneServer server = entry.getValue();
        final URI serverURI = server.getContext().get(URI.class);
        logger.info(String.format("Starting %s server", name));
        server.start(1, TimeUnit.MINUTES);
        services.add(serverURI);
        logger.info("Waiting for updated list");
        services.assertServices(10, TimeUnit.SECONDS, new CalculatorCallable(bean), 500);
        logger.info("Asserting balance");
        assertBalance(bean, services.get().size());
    }
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) URI(java.net.URI) InitialContext(javax.naming.InitialContext) StandaloneServer(org.apache.openejb.server.control.StandaloneServer) Calculator(org.apache.openejb.itest.failover.ejb.Calculator) RemoteInitialContextFactory(org.apache.openejb.client.RemoteInitialContextFactory) EJBException(javax.ejb.EJBException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 37 with EJBException

use of javax.ejb.EJBException in project tomee by apache.

the class StickyConnectionStrategyTest method test.

@Test
public void test() throws Exception {
    // To run in an IDE, uncomment and update this line
    System.setProperty("version", OpenEjbVersion.get().getVersion());
    final File zip = Repository.getArtifact("org.apache.tomee", "openejb-standalone", "zip");
    final File app = Repository.getArtifact("org.apache.openejb.itests", "failover-ejb", "jar");
    final File dir = Files.tmpdir();
    final StandaloneServer root;
    {
        final StandaloneServer root1;
        final String name = "root";
        final File home = new File(dir, name);
        Files.mkdir(home);
        Zips.unzip(zip, home, true);
        root1 = new StandaloneServer(home, home);
        root1.killOnExit();
        root1.ignoreOut();
        root1.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb");
        root1.setProperty("name", name);
        root1.setProperty("openejb.extract.configuration", "false");
        final StandaloneServer.ServerService multipoint = root1.getServerService("multipoint");
        multipoint.setBind("localhost");
        multipoint.setPort(getAvailablePort());
        multipoint.setDisabled(false);
        multipoint.set("discoveryName", name);
        root = root1;
        logger.info("Starting Root server");
        root.start();
    }
    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.setProperty("name", name);
        server.setProperty("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.setBind("localhost");
        ejbd.setDisabled(false);
        ejbd.setPort(getAvailablePort());
        ejbd.setThreads(5);
        ejbd.set("discovery", "ejb:ejbd://{bind}:{port}/" + name);
        final StandaloneServer.ServerService multipoint = server.getServerService("multipoint");
        multipoint.setPort(getAvailablePort());
        multipoint.setDisabled(false);
        multipoint.set("discoveryName", name);
        multipoint.set("initialServers", "localhost:" + root.getServerService("multipoint").getPort());
        servers.put(name, server);
        logger.info(String.format("Starting %s server", name));
        server.start(1, TimeUnit.MINUTES);
        invoke(name, server);
    }
    System.setProperty("openejb.client.requestretry", "true");
    logger.info("Beginning Test");
    final Properties environment = new Properties();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
    environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + servers.values().iterator().next().getServerService("ejbd").getPort() + "/provider");
    final InitialContext context = new InitialContext(environment);
    final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");
    String previous = null;
    for (final StandaloneServer ignored : servers.values()) {
        logger.info("Looping");
        // What server are we talking to now?
        final String name = bean.name();
        logger.info("Sticky request to " + name);
        // The root should not be serving apps
        assertFalse("root".equals(name));
        // Should not be the same server we were talking with previously (we killed that server)
        if (previous != null)
            assertFalse(name.equals(previous));
        previous = name;
        final int i = 1000;
        logger.info(String.format("Performing %s invocations, expecting %s to be used for each invocation.", i, name));
        // Should be the same server for the next N calls
        invoke(bean, i, name);
        logger.info("Shutting down " + name);
        // Now let's kill that server
        servers.get(name).kill();
    }
    logger.info("All Servers Shutdown");
    try {
        logger.info("Making one last request, expecting complete failover");
        final String name = bean.name();
        Assert.fail("Server should be destroyed: " + name);
    } catch (final EJBException e) {
        logger.info(String.format("Pass.  Request resulted in %s: %s", e.getCause().getClass().getSimpleName(), e.getMessage()));
    // good
    }
    // Let's start a server again and invocations should now succeed
    final Iterator<StandaloneServer> iterator = servers.values().iterator();
    iterator.next();
    final StandaloneServer server = iterator.next();
    logger.info(String.format("Starting %s server", server.getProperties().get("name")));
    server.start(1, TimeUnit.MINUTES);
    logger.info("Performing one more invocation");
    assertEquals(5, bean.sum(2, 3));
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) StandaloneServer(org.apache.openejb.server.control.StandaloneServer) Calculator(org.apache.openejb.itest.failover.ejb.Calculator) RemoteInitialContextFactory(org.apache.openejb.client.RemoteInitialContextFactory) EJBException(javax.ejb.EJBException) File(java.io.File) Test(org.junit.Test)

Example 38 with EJBException

use of javax.ejb.EJBException in project tomee by apache.

the class LegacyServerTest method test.

@Test
public void test() throws Exception {
    final String jv = System.getProperty("java.version");
    assumeTrue(jv.startsWith("1.7") || jv.startsWith("1.8"));
    // To run in an IDE, uncomment and update this line
    // System.setProperty("version", OpenEjbVersion.get().getVersion());
    System.setProperty("openejb.client.connection.strategy", "roundrobin");
    logger.info("Retrieving standalone server: " + Repository.guessVersion("org.apache.openejb", "openejb-standalone") + " - This may take a while...");
    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();
    final StandaloneServer root;
    {
        final String name = "root";
        final File home = new File(dir, name);
        Files.mkdir(home);
        Zips.unzip(zip, home, true);
        root = new StandaloneServer(home, home);
        root.killOnExit();
        root.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb");
        root.ignoreOut();
        root.setProperty("name", name);
        root.setProperty("openejb.extract.configuration", "false");
        final StandaloneServer.ServerService multipoint = root.getServerService("multipoint");
        multipoint.setBind("localhost");
        multipoint.setPort(getNextAvailablePort());
        multipoint.setDisabled(false);
        multipoint.set("discoveryName", name);
        logger.info("Starting Root server");
        // Wait for it to start before continuing, otherwise test may fail in slower machines.
        root.start(1, TimeUnit.MINUTES);
    }
    final Services services = new Services();
    Client.addEventObserver(services);
    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.setProperty("name", name);
        server.setProperty("openejb.extract.configuration", "false");
        server.getJvmOpts().add("-Dopenejb.classloader.forced-load=org.apache.openejb");
        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.setBind("localhost");
        ejbd.setDisabled(false);
        ejbd.setPort(getNextAvailablePort());
        ejbd.setThreads(5);
        final URI uri = URI.create(String.format("ejbd://%s:%s/%s", ejbd.getBind(), ejbd.getPort(), name));
        ejbd.set("discovery", "ejb:" + uri);
        services.add(uri);
        server.getContext().set(URI.class, uri);
        final StandaloneServer.ServerService multipoint = server.getServerService("multipoint");
        multipoint.setPort(getNextAvailablePort());
        multipoint.setDisabled(false);
        multipoint.set("discoveryName", name);
        multipoint.set("initialServers", "localhost:" + root.getServerService("multipoint").getPort());
        servers.put(name, server);
        logger.info(String.format("Starting %s server", name));
        server.start(1, TimeUnit.MINUTES);
    }
    System.setProperty("openejb.client.requestretry", "true");
    System.setProperty("openejb.client.connection.strategy", "random");
    logger.info("Beginning Test");
    final Properties environment = new Properties();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
    environment.put(Context.PROVIDER_URL, "ejbd://localhost:" + servers.values().iterator().next().getServerService("ejbd").getPort() + "/provider");
    final InitialContext context = new InitialContext(environment);
    final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");
    for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
        final String name = entry.getKey();
        final StandaloneServer server = entry.getValue();
        final URI serverURI = server.getContext().get(URI.class);
        logger.info("Waiting for updated list");
        services.assertServices(CLIENT_DELAY, TimeUnit.SECONDS, new CalculatorCallable(bean), 500);
        logger.info("Asserting balance");
        assertBalance(bean, services.get().size());
        logger.info("Shutting down " + name);
        server.kill();
        services.remove(serverURI);
    }
    logger.info("All Servers Shutdown");
    try {
        logger.info("Making one last request, expecting complete failover");
        final String name = bean.name();
        Assert.fail("Server should be destroyed: " + name);
    } catch (final EJBException e) {
        logger.info(String.format("Pass.  Request resulted in %s: %s", e.getCause().getClass().getSimpleName(), e.getMessage()));
    // good
    }
    for (final Map.Entry<String, StandaloneServer> entry : servers.entrySet()) {
        final String name = entry.getKey();
        final StandaloneServer server = entry.getValue();
        final URI serverURI = server.getContext().get(URI.class);
        logger.info(String.format("Starting %s server", name));
        server.start(1, TimeUnit.MINUTES);
        services.add(serverURI);
        logger.info("Waiting for updated list");
        services.assertServices(CLIENT_DELAY, TimeUnit.SECONDS, new CalculatorCallable(bean), 500);
        logger.info("Asserting balance");
        assertBalance(bean, services.get().size());
    }
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) URI(java.net.URI) InitialContext(javax.naming.InitialContext) StandaloneServer(org.apache.openejb.server.control.StandaloneServer) Calculator(org.apache.openejb.itest.failover.ejb.Calculator) RemoteInitialContextFactory(org.apache.openejb.client.RemoteInitialContextFactory) EJBException(javax.ejb.EJBException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 39 with EJBException

use of javax.ejb.EJBException in project tomee by apache.

the class DatabaseBean method execute.

public boolean execute(final String statement) throws java.sql.SQLException {
    boolean retval;
    Connection con = null;
    try {
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
        con = ds.getConnection();
        final Statement stmt = con.createStatement();
        try {
            retval = stmt.execute(statement);
        } finally {
            stmt.close();
        }
    } catch (final javax.naming.NamingException e) {
        // throw new RemoteException("Cannot execute the statement: "+statement, e);
        throw new EJBException("Cannot lookup the Database bean." + e.getMessage());
    } finally {
        if (con != null) {
            con.close();
        }
    }
    return retval;
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) EJBException(javax.ejb.EJBException) DataSource(javax.sql.DataSource)

Example 40 with EJBException

use of javax.ejb.EJBException in project tomee by apache.

the class DatabaseBean method executeQuery.

public void executeQuery(final String statement) throws java.sql.SQLException {
    try {
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
        final Connection con = ds.getConnection();
        try {
            final PreparedStatement stmt = con.prepareStatement(statement);
            try {
                stmt.executeQuery();
            } finally {
                stmt.close();
            }
        } finally {
            con.close();
        }
    } catch (final Exception e) {
        throw new EJBException("Cannot execute the statement: " + statement + e.getMessage());
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) EJBException(javax.ejb.EJBException) EJBException(javax.ejb.EJBException) DataSource(javax.sql.DataSource)

Aggregations

EJBException (javax.ejb.EJBException)169 CreateException (javax.ejb.CreateException)51 RemoteException (java.rmi.RemoteException)45 RemoveException (javax.ejb.RemoveException)40 InitialContext (javax.naming.InitialContext)40 NoSuchObjectLocalException (javax.ejb.NoSuchObjectLocalException)37 SystemException (javax.transaction.SystemException)37 FinderException (javax.ejb.FinderException)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)24 OpenEJBException (org.apache.openejb.OpenEJBException)24 EJBObject (javax.ejb.EJBObject)18 NoSuchEntityException (javax.ejb.NoSuchEntityException)17 IOException (java.io.IOException)16 Test (org.junit.Test)16 EjbInvocation (com.sun.ejb.EjbInvocation)14 ConcurrentAccessException (javax.ejb.ConcurrentAccessException)14 IllegalLoopbackException (javax.ejb.IllegalLoopbackException)14 NamingException (javax.naming.NamingException)14 NotSerializableException (java.io.NotSerializableException)13 ConcurrentAccessTimeoutException (javax.ejb.ConcurrentAccessTimeoutException)13