use of javax.ejb.EJBException in project tomee by apache.
the class EntityManagerPropogationTest method _testSFTr2SFEx.
public void _testSFTr2SFEx() throws Exception {
final InitialContext ctx = new InitialContext();
final Node node = (Node) ctx.lookup("TransactionToExtendedLocalBean");
try {
// System.out.println("SFSB+TPC --> SFSB+EPC");
node.createUntilLeaf(5, "Yellow");
fail("5.6.3.1 Requirements for Persistence Context Propagation (persistence spec)" + "\n\t--> we cannot have two persistence contexts associated with the transaction");
} catch (final EJBException e) {
// OK
// System.out.println(e.getMessage());
}
}
use of javax.ejb.EJBException in project tomee by apache.
the class StatelessInterceptorTest method test.
public void test() throws Exception {
final Target target = (Target) ctx.lookup("TargetBeanLocal");
target.echo(new ArrayList());
assertCalls(Call.values());
calls.clear();
int i = target.echo(123);
assertEquals(123, i);
assertCalls(Call.Default_Invoke_BEFORE, Call.Method_ann_Invoke_BEFORE, Call.Method_dd_Invoke_BEFORE, Call.Bean_Invoke_BEFORE, Call.Bean_Invoke, Call.Bean_Invoke_AFTER, Call.Method_dd_Invoke_AFTER, Call.Method_ann_Invoke_AFTER, Call.Default_Invoke_AFTER);
calls.clear();
boolean b = target.echo(true);
assertTrue(b);
assertCalls(Call.Method_ann_Invoke_BEFORE, Call.Method_dd_Invoke_BEFORE, Call.Bean_Invoke_BEFORE, Call.Bean_Invoke, Call.Bean_Invoke_AFTER, Call.Method_dd_Invoke_AFTER, Call.Method_ann_Invoke_AFTER);
calls.clear();
try {
target.throwAppException();
fail("Should have thrown app exception");
} catch (final AppException e) {
// pass
}
try {
target.throwSysException();
fail("Should have thrown a sys exception");
} catch (final EJBException e) {
// so far so good
final Throwable cause = e.getCause();
if (!(cause instanceof SysException)) {
fail("Inner Exception should be a SysException");
}
}
calls.clear();
final Target target2 = (Target) ctx.lookup("Target2BeanLocal");
i = target2.echo(123);
assertEquals(123, i);
calls.clear();
i = target2.echo(123);
assertEquals(123, i);
assertCalls(Call.Method_ann_Invoke_BEFORE, Call.Bean_Invoke_BEFORE, Call.Bean_Invoke, Call.Bean_Invoke_AFTER, Call.Method_ann_Invoke_AFTER);
calls.clear();
b = target2.echo(true);
assertTrue(b);
assertCalls(Call.Method_ann_Invoke_BEFORE, Call.Bean_Invoke_BEFORE, Call.Bean_Invoke, Call.Bean_Invoke_AFTER, Call.Method_ann_Invoke_AFTER);
calls.clear();
}
use of javax.ejb.EJBException in project tomee by apache.
the class StorageBean method setBytes.
public void setBytes(final byte[] bytes) {
try {
final DataSource ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/DefaultDatabase");
final Connection c = ds.getConnection();
final PreparedStatement ps = c.prepareStatement("UPDATE storage SET blob_column = ? WHERE id = ?");
ps.setBinaryStream(1, new ByteArrayInputStream(bytes), bytes.length);
ps.setInt(2, ((Integer) ctx.getPrimaryKey()).intValue());
ps.executeUpdate();
ps.close();
c.close();
} catch (final Exception e) {
throw new EJBException(e);
}
}
use of javax.ejb.EJBException in project tomee by apache.
the class DiscoveryHostTest method test.
@Ignore
@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 List<StandaloneServer> servers = new ArrayList<StandaloneServer>();
final List<String> initialServers = new ArrayList<String>();
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");
initialServers.add("localhost:" + multipoint.getPort());
servers.add(server);
}
servers.get(0).setOut(System.out);
for (final StandaloneServer server : servers) {
final StandaloneServer.ServerService multipoint = server.getServerService("multipoint");
multipoint.set("initialServers", Join.join(",", initialServers));
}
for (final StandaloneServer server : servers) {
server.start(1, TimeUnit.MINUTES);
}
Collections.reverse(servers);
System.setProperty("openejb.client.requestretry", "true");
final Properties environment = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
environment.put(Context.PROVIDER_URL, "failover:ejbd://localhost:" + servers.get(0).getServerService("ejbd").getPort());
final InitialContext context = new InitialContext(environment);
final Calculator bean = (Calculator) context.lookup("CalculatorBeanRemote");
for (final StandaloneServer server : servers) {
System.out.println(String.format("Average invocation time %s microseconds", invoke(bean, 10000)));
server.kill();
}
System.out.println("All servers destroyed");
try {
System.out.println(String.format("Average invocation time %s microseconds", invoke(bean, 10000)));
Assert.fail("Server should be destroyed");
} catch (final EJBException e) {
// good
}
for (final StandaloneServer server : servers) {
server.start(1, TimeUnit.MINUTES);
System.out.println(String.format("Average invocation time %s microseconds", invoke(bean, 10000)));
}
System.out.println("DONE");
}
use of javax.ejb.EJBException in project tomee by apache.
the class RandomConnectionStrategyTest 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.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());
}
}
Aggregations