http://git-wip-us.apache.org/repos/asf/tomee-tomee-site-generator/blob/972cc356/src/main/jbake/content/examples/transaction-rollback.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/transaction-rollback.adoc b/src/main/jbake/content/examples/transaction-rollback.adoc new file mode 100755 index 0000000..6b8088d --- /dev/null +++ b/src/main/jbake/content/examples/transaction-rollback.adoc @@ -0,0 +1,584 @@ += Transaction Rollback +:jbake-date: 2016-09-06 +:jbake-type: page +:jbake-tomeepdf: +:jbake-status: published + +Example transaction-rollback can be browsed at https://github.com/apache/tomee/tree/master/examples/transaction-rollback + + +*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* + +== CustomRuntimeException + + +[source,java] +---- +package org.superbiz.txrollback; + +import javax.ejb.ApplicationException; + +@ApplicationException +public class CustomRuntimeException extends RuntimeException { + + public CustomRuntimeException() { + } + + public CustomRuntimeException(String s) { + super(s); + } + + public CustomRuntimeException(String s, Throwable throwable) { + super(s, throwable); + } + + public CustomRuntimeException(Throwable throwable) { + super(throwable); + } +} +---- + + +== Movie + + +[source,java] +---- +package org.superbiz.txrollback; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity(name = "Movie") +public class Movie { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String director; + private String title; + private int year; + + public Movie() { + } + + public Movie(String director, String title, int year) { + this.director = director; + this.title = title; + this.year = year; + } + + public String getDirector() { + return director; + } + + public void setDirector(String director) { + this.director = director; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + +} +---- + + +== Movies + + +[source,java] +---- +package org.superbiz.txrollback; + +import javax.annotation.Resource; +import javax.ejb.SessionContext; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import javax.persistence.Query; +import java.util.List; + +//START SNIPPET: code +@Stateless +public class Movies { + + @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.TRANSACTION) + private EntityManager entityManager; + + @Resource + private SessionContext sessionContext; + + public void addMovie(Movie movie) throws Exception { + entityManager.persist(movie); + } + + public void deleteMovie(Movie movie) throws Exception { + entityManager.remove(movie); + } + + public List<Movie> getMovies() throws Exception { + Query query = entityManager.createQuery("SELECT m from Movie as m"); + return query.getResultList(); + } + + public void callSetRollbackOnly() { + sessionContext.setRollbackOnly(); + } + + public void throwUncheckedException() { + throw new RuntimeException("Throwing unchecked exceptions will rollback a transaction"); + } + + public void throwApplicationException() { + throw new CustomRuntimeException("This is marked @ApplicationException, so no TX rollback"); + } +} +---- + + +== persistence.xml + + +[source,xml] +---- +<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> + + <persistence-unit name="movie-unit"> + <jta-data-source>movieDatabase</jta-data-source> + <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source> + <class>org.superbiz.testinjection.MoviesTest.Movie</class> + + <properties> + <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> + </properties> + </persistence-unit> +</persistence> +---- + + +== MoviesTest + + +[source,java] +---- +package org.superbiz.txrollback; + +import junit.framework.TestCase; + +import javax.annotation.Resource; +import javax.ejb.EJB; +import javax.ejb.embeddable.EJBContainer; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.transaction.RollbackException; +import javax.transaction.UserTransaction; +import java.util.List; +import java.util.Properties; + +//START SNIPPET: code +public class MoviesTest extends TestCase { + + @EJB + private Movies movies; + + @Resource + private UserTransaction userTransaction; + + @PersistenceContext + private EntityManager entityManager; + + private EJBContainer ejbContainer; + + public void setUp() throws Exception { + Properties p = new Properties(); + p.put("movieDatabase", "new://Resource?type=DataSource"); + p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver"); + p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb" + System.currentTimeMillis()); + + ejbContainer = EJBContainer.createEJBContainer(p); + ejbContainer.getContext().bind("inject", this); + } + + @Override + protected void tearDown() throws Exception { + ejbContainer.close(); + } + + /** + * Standard successful transaction scenario. The data created inside + * the transaction is visible after the transaction completes. + * <p/> + * Note that UserTransaction is only usable by Bean-Managed Transaction + * beans, which can be specified with @TransactionManagement(BEAN) + */ + public void testCommit() throws Exception { + + userTransaction.begin(); + + try { + entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); + entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); + entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); + + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + } finally { + userTransaction.commit(); + } + + // Transaction was committed + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + } + + /** + * Standard transaction rollback scenario. The data created inside + * the transaction is not visible after the transaction completes. + */ + public void testUserTransactionRollback() throws Exception { + + userTransaction.begin(); + + try { + entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); + entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); + entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); + + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + } finally { + userTransaction.rollback(); + } + + // Transaction was rolled back + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 0, list.size()); + } + + /** + * Transaction is marked for rollback inside the bean via + * calling the javax.ejb.SessionContext.setRollbackOnly() method + * <p/> + * This is the cleanest way to make a transaction rollback. + */ + public void testMarkedRollback() throws Exception { + + userTransaction.begin(); + + try { + entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); + entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); + entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); + + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + + movies.callSetRollbackOnly(); + } finally { + try { + userTransaction.commit(); + fail("A RollbackException should have been thrown"); + } catch (RollbackException e) { + // Pass + } + } + + // Transaction was rolled back + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 0, list.size()); + } + + /** + * Throwing an unchecked exception from a bean will cause + * the container to call setRollbackOnly() and discard the + * bean instance from further use without calling any @PreDestroy + * methods on the bean instance. + */ + public void testExceptionBasedRollback() throws Exception { + + userTransaction.begin(); + + try { + entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); + entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); + entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); + + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + + try { + movies.throwUncheckedException(); + } catch (RuntimeException e) { + // Good, this will cause the tx to rollback + } + } finally { + try { + userTransaction.commit(); + fail("A RollbackException should have been thrown"); + } catch (RollbackException e) { + // Pass + } + } + + // Transaction was rolled back + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 0, list.size()); + } + + /** + * It is still possible to throw unchecked (runtime) exceptions + * without dooming the transaction by marking the exception + * with the @ApplicationException annotation or in the ejb-jar.xml + * deployment descriptor via the <application-exception> tag + */ + public void testCommit2() throws Exception { + + userTransaction.begin(); + + try { + entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); + entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); + entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); + + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + + try { + movies.throwApplicationException(); + } catch (RuntimeException e) { + // This will *not* cause the tx to rollback + // because it is marked as an @ApplicationException + } + } finally { + userTransaction.commit(); + } + + // Transaction was committed + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + } +} +---- + + += Running + + + +[source] +---- +------------------------------------------------------- + T E S T S +------------------------------------------------------- +Running org.superbiz.txrollback.MoviesTest +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +INFO - openejb.home = /Users/dblevins/examples/transaction-rollback +INFO - openejb.base = /Users/dblevins/examples/transaction-rollback +INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) +INFO - Found EjbModule in classpath: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Beginning load: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Configuring enterprise application: /Users/dblevins/examples/transaction-rollback +WARN - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime. +INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +INFO - Auto-creating a container for bean Movies: Container(type=STATELESS, id=Default Stateless Container) +INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +INFO - Auto-creating a container for bean org.superbiz.txrollback.MoviesTest: Container(type=MANAGED, id=Default Managed Container) +INFO - Configuring PersistenceUnit(name=movie-unit) +INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. +INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) +INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' +INFO - Enterprise application "/Users/dblevins/examples/transaction-rollback" loaded. +INFO - Assembling app: /Users/dblevins/examples/transaction-rollback +INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 412ms +INFO - Jndi(name="java:global/transaction-rollback/Movies!org.superbiz.txrollback.Movies") +INFO - Jndi(name="java:global/transaction-rollback/Movies") +INFO - Jndi(name="java:global/EjbModule1718375554/org.superbiz.txrollback.MoviesTest!org.superbiz.txrollback.MoviesTest") +INFO - Jndi(name="java:global/EjbModule1718375554/org.superbiz.txrollback.MoviesTest") +INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Created Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Started Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Deployed Application(path=/Users/dblevins/examples/transaction-rollback) +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +INFO - Undeploying app: /Users/dblevins/examples/transaction-rollback +INFO - Closing DataSource: movieDatabase +INFO - Closing DataSource: movieDatabaseNonJta +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +INFO - openejb.home = /Users/dblevins/examples/transaction-rollback +INFO - openejb.base = /Users/dblevins/examples/transaction-rollback +INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) +INFO - Found EjbModule in classpath: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Beginning load: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Configuring enterprise application: /Users/dblevins/examples/transaction-rollback +INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +INFO - Auto-creating a container for bean Movies: Container(type=STATELESS, id=Default Stateless Container) +INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +INFO - Auto-creating a container for bean org.superbiz.txrollback.MoviesTest: Container(type=MANAGED, id=Default Managed Container) +INFO - Configuring PersistenceUnit(name=movie-unit) +INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. +INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) +INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' +INFO - Enterprise application "/Users/dblevins/examples/transaction-rollback" loaded. +INFO - Assembling app: /Users/dblevins/examples/transaction-rollback +INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 5ms +INFO - Jndi(name="java:global/transaction-rollback/Movies!org.superbiz.txrollback.Movies") +INFO - Jndi(name="java:global/transaction-rollback/Movies") +INFO - Jndi(name="java:global/EjbModule935567559/org.superbiz.txrollback.MoviesTest!org.superbiz.txrollback.MoviesTest") +INFO - Jndi(name="java:global/EjbModule935567559/org.superbiz.txrollback.MoviesTest") +INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Created Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Started Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Deployed Application(path=/Users/dblevins/examples/transaction-rollback) +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +INFO - Undeploying app: /Users/dblevins/examples/transaction-rollback +INFO - Closing DataSource: movieDatabase +INFO - Closing DataSource: movieDatabaseNonJta +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +INFO - openejb.home = /Users/dblevins/examples/transaction-rollback +INFO - openejb.base = /Users/dblevins/examples/transaction-rollback +INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) +INFO - Found EjbModule in classpath: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Beginning load: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Configuring enterprise application: /Users/dblevins/examples/transaction-rollback +INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +INFO - Auto-creating a container for bean Movies: Container(type=STATELESS, id=Default Stateless Container) +INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +INFO - Auto-creating a container for bean org.superbiz.txrollback.MoviesTest: Container(type=MANAGED, id=Default Managed Container) +INFO - Configuring PersistenceUnit(name=movie-unit) +INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. +INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) +INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' +INFO - Enterprise application "/Users/dblevins/examples/transaction-rollback" loaded. +INFO - Assembling app: /Users/dblevins/examples/transaction-rollback +INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 5ms +INFO - Jndi(name="java:global/transaction-rollback/Movies!org.superbiz.txrollback.Movies") +INFO - Jndi(name="java:global/transaction-rollback/Movies") +INFO - Jndi(name="java:global/EjbModule1961109485/org.superbiz.txrollback.MoviesTest!org.superbiz.txrollback.MoviesTest") +INFO - Jndi(name="java:global/EjbModule1961109485/org.superbiz.txrollback.MoviesTest") +INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Created Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Started Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Deployed Application(path=/Users/dblevins/examples/transaction-rollback) +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +INFO - Undeploying app: /Users/dblevins/examples/transaction-rollback +INFO - Closing DataSource: movieDatabase +INFO - Closing DataSource: movieDatabaseNonJta +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +INFO - openejb.home = /Users/dblevins/examples/transaction-rollback +INFO - openejb.base = /Users/dblevins/examples/transaction-rollback +INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) +INFO - Found EjbModule in classpath: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Beginning load: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Configuring enterprise application: /Users/dblevins/examples/transaction-rollback +INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +INFO - Auto-creating a container for bean Movies: Container(type=STATELESS, id=Default Stateless Container) +INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +INFO - Auto-creating a container for bean org.superbiz.txrollback.MoviesTest: Container(type=MANAGED, id=Default Managed Container) +INFO - Configuring PersistenceUnit(name=movie-unit) +INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. +INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) +INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' +INFO - Enterprise application "/Users/dblevins/examples/transaction-rollback" loaded. +INFO - Assembling app: /Users/dblevins/examples/transaction-rollback +INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 5ms +INFO - Jndi(name="java:global/transaction-rollback/Movies!org.superbiz.txrollback.Movies") +INFO - Jndi(name="java:global/transaction-rollback/Movies") +INFO - Jndi(name="java:global/EjbModule419651577/org.superbiz.txrollback.MoviesTest!org.superbiz.txrollback.MoviesTest") +INFO - Jndi(name="java:global/EjbModule419651577/org.superbiz.txrollback.MoviesTest") +INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Created Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Started Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Deployed Application(path=/Users/dblevins/examples/transaction-rollback) +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +INFO - Undeploying app: /Users/dblevins/examples/transaction-rollback +INFO - Closing DataSource: movieDatabase +INFO - Closing DataSource: movieDatabaseNonJta +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +INFO - openejb.home = /Users/dblevins/examples/transaction-rollback +INFO - openejb.base = /Users/dblevins/examples/transaction-rollback +INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) +INFO - Found EjbModule in classpath: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Beginning load: /Users/dblevins/examples/transaction-rollback/target/classes +INFO - Configuring enterprise application: /Users/dblevins/examples/transaction-rollback +INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +INFO - Auto-creating a container for bean Movies: Container(type=STATELESS, id=Default Stateless Container) +INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +INFO - Auto-creating a container for bean org.superbiz.txrollback.MoviesTest: Container(type=MANAGED, id=Default Managed Container) +INFO - Configuring PersistenceUnit(name=movie-unit) +INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. +INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) +INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' +INFO - Enterprise application "/Users/dblevins/examples/transaction-rollback" loaded. +INFO - Assembling app: /Users/dblevins/examples/transaction-rollback +INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 4ms +INFO - Jndi(name="java:global/transaction-rollback/Movies!org.superbiz.txrollback.Movies") +INFO - Jndi(name="java:global/transaction-rollback/Movies") +INFO - Jndi(name="java:global/EjbModule15169271/org.superbiz.txrollback.MoviesTest!org.superbiz.txrollback.MoviesTest") +INFO - Jndi(name="java:global/EjbModule15169271/org.superbiz.txrollback.MoviesTest") +INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Created Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +INFO - Started Ejb(deployment-id=org.superbiz.txrollback.MoviesTest, ejb-name=org.superbiz.txrollback.MoviesTest, container=Default Managed Container) +INFO - Deployed Application(path=/Users/dblevins/examples/transaction-rollback) +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@39172e08; ignoring. +INFO - Undeploying app: /Users/dblevins/examples/transaction-rollback +INFO - Closing DataSource: movieDatabase +INFO - Closing DataSource: movieDatabaseNonJta +Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.586 sec + +Results : + +Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 +---- + +
http://git-wip-us.apache.org/repos/asf/tomee-tomee-site-generator/blob/972cc356/src/main/jbake/content/examples/troubleshooting.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/troubleshooting.adoc b/src/main/jbake/content/examples/troubleshooting.adoc new file mode 100755 index 0000000..681f79f --- /dev/null +++ b/src/main/jbake/content/examples/troubleshooting.adoc @@ -0,0 +1,480 @@ += Troubleshooting +:jbake-date: 2016-09-06 +:jbake-type: page +:jbake-tomeepdf: +:jbake-status: published + +Example troubleshooting can be browsed at https://github.com/apache/tomee/tree/master/examples/troubleshooting + + +*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* + +== Movie + + +[source,java] +---- +package org.superbiz.troubleshooting; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity(name = "Movie") +public class Movie { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String director; + private String title; + private int year; + + public Movie() { + } + + public Movie(String director, String title, int year) { + this.director = director; + this.title = title; + this.year = year; + } + + public String getDirector() { + return director; + } + + public void setDirector(String director) { + this.director = director; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + +} +---- + + +== Movies + + +[source,java] +---- +package org.superbiz.troubleshooting; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import javax.persistence.Query; +import java.util.List; + +//START SNIPPET: code +@Stateless +public class Movies { + + @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.TRANSACTION) + private EntityManager entityManager; + + public void addMovie(Movie movie) throws Exception { + entityManager.persist(movie); + } + + public void deleteMovie(Movie movie) throws Exception { + entityManager.remove(movie); + } + + public List<Movie> getMovies() throws Exception { + Query query = entityManager.createQuery("SELECT m from Movie as m"); + return query.getResultList(); + } +} +---- + + +== persistence.xml + + +[source,xml] +---- +<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> + + <persistence-unit name="movie-unit"> + <jta-data-source>movieDatabase</jta-data-source> + <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source> + <class>org.superbiz.testinjection.MoviesTest.Movie</class> + + <properties> + <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> + </properties> + </persistence-unit> +</persistence> +---- + + +== MoviesTest + + +[source,java] +---- +package org.superbiz.troubleshooting; + +import junit.framework.TestCase; + +import javax.annotation.Resource; +import javax.ejb.EJB; +import javax.ejb.embeddable.EJBContainer; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.transaction.UserTransaction; +import java.util.List; +import java.util.Properties; + +//START SNIPPET: code +public class MoviesTest extends TestCase { + + @EJB + private Movies movies; + + @Resource + private UserTransaction userTransaction; + + @PersistenceContext + private EntityManager entityManager; + + public void setUp() throws Exception { + Properties p = new Properties(); + p.put("movieDatabase", "new://Resource?type=DataSource"); + p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver"); + + // These two debug levels will get you the basic log information + // on the deployment of applications. Good first step in troubleshooting. + p.put("log4j.category.OpenEJB.startup", "debug"); + p.put("log4j.category.OpenEJB.startup.config", "debug"); + + // This log category is a good way to see what "openejb.foo" options + // and flags are available and what their default values are + p.put("log4j.category.OpenEJB.options", "debug"); + + // This will output the full configuration of all containers + // resources and other openejb.xml configurable items. A good + // way to see what the final configuration looks like after all + // overriding has been applied. + p.put("log4j.category.OpenEJB.startup.service", "debug"); + + // Will output a generated ejb-jar.xml file that represents + // 100% of the annotations used in the code. This is a great + // way to figure out how to do something in xml for overriding + // or just to "see" all your application meta-data in one place. + // Look for log lines like this "Dumping Generated ejb-jar.xml to" + p.put("openejb.descriptors.output", "true"); + + // Setting the validation output level to verbose results in + // validation messages that attempt to provide explanations + // and information on what steps can be taken to remedy failures. + // A great tool for those learning EJB. + p.put("openejb.validation.output.level", "verbose"); + + EJBContainer.createEJBContainer(p).getContext().bind("inject", this); + } + + public void test() throws Exception { + + userTransaction.begin(); + + try { + entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992)); + entityManager.persist(new Movie("Joel Coen", "Fargo", 1996)); + entityManager.persist(new Movie("Joel Coen", "The Big Lebowski", 1998)); + + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + } finally { + userTransaction.commit(); + } + + // Transaction was committed + List<Movie> list = movies.getMovies(); + assertEquals("List.size()", 3, list.size()); + } +} +---- + + += Running + + + +[source] +---- +------------------------------------------------------- + T E S T S +------------------------------------------------------- +Running org.superbiz.troubleshooting.MoviesTest +2011-10-29 11:50:19,482 - DEBUG - Using default 'openejb.nobanner=true' +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +2011-10-29 11:50:19,482 - INFO - openejb.home = /Users/dblevins/examples/troubleshooting +2011-10-29 11:50:19,482 - INFO - openejb.base = /Users/dblevins/examples/troubleshooting +2011-10-29 11:50:19,483 - DEBUG - Using default 'openejb.assembler=org.apache.openejb.assembler.classic.Assembler' +2011-10-29 11:50:19,483 - DEBUG - Instantiating assembler class org.apache.openejb.assembler.classic.Assembler +2011-10-29 11:50:19,517 - DEBUG - Using default 'openejb.jndiname.failoncollision=true' +2011-10-29 11:50:19,517 - INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +2011-10-29 11:50:19,520 - DEBUG - Using default 'openejb.configurator=org.apache.openejb.config.ConfigurationFactory' +2011-10-29 11:50:19,588 - DEBUG - Using default 'openejb.validation.skip=false' +2011-10-29 11:50:19,589 - DEBUG - Using default 'openejb.deploymentId.format={ejbName}' +2011-10-29 11:50:19,589 - DEBUG - Using default 'openejb.debuggable-vm-hackery=false' +2011-10-29 11:50:19,589 - DEBUG - Using default 'openejb.webservices.enabled=true' +2011-10-29 11:50:19,594 - DEBUG - Using default 'openejb.vendor.config=ALL' Possible values are: geronimo, glassfish, jboss, weblogic or NONE or ALL +2011-10-29 11:50:19,612 - DEBUG - Using default 'openejb.provider.default=org.apache.openejb.embedded' +2011-10-29 11:50:19,658 - INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +2011-10-29 11:50:19,662 - INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +2011-10-29 11:50:19,665 - INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database) +2011-10-29 11:50:19,665 - DEBUG - Override [JdbcDriver=org.hsqldb.jdbcDriver] +2011-10-29 11:50:19,666 - DEBUG - Using default 'openejb.deployments.classpath=false' +2011-10-29 11:50:19,666 - INFO - Creating TransactionManager(id=Default Transaction Manager) +2011-10-29 11:50:19,676 - DEBUG - defaultTransactionTimeoutSeconds=600 +2011-10-29 11:50:19,676 - DEBUG - TxRecovery=false +2011-10-29 11:50:19,676 - DEBUG - bufferSizeKb=32 +2011-10-29 11:50:19,676 - DEBUG - checksumEnabled=true +2011-10-29 11:50:19,676 - DEBUG - adler32Checksum=true +2011-10-29 11:50:19,676 - DEBUG - flushSleepTimeMilliseconds=50 +2011-10-29 11:50:19,676 - DEBUG - logFileDir=txlog +2011-10-29 11:50:19,676 - DEBUG - logFileExt=log +2011-10-29 11:50:19,676 - DEBUG - logFileName=howl +2011-10-29 11:50:19,676 - DEBUG - maxBlocksPerFile=-1 +2011-10-29 11:50:19,677 - DEBUG - maxBuffers=0 +2011-10-29 11:50:19,677 - DEBUG - maxLogFiles=2 +2011-10-29 11:50:19,677 - DEBUG - minBuffers=4 +2011-10-29 11:50:19,677 - DEBUG - threadsWaitingForceThreshold=-1 +2011-10-29 11:50:19,724 - DEBUG - createService.success +2011-10-29 11:50:19,724 - INFO - Creating SecurityService(id=Default Security Service) +2011-10-29 11:50:19,724 - DEBUG - DefaultUser=guest +2011-10-29 11:50:19,750 - DEBUG - createService.success +2011-10-29 11:50:19,750 - INFO - Creating Resource(id=movieDatabase) +2011-10-29 11:50:19,750 - DEBUG - Definition= +2011-10-29 11:50:19,750 - DEBUG - JtaManaged=true +2011-10-29 11:50:19,750 - DEBUG - JdbcDriver=org.hsqldb.jdbcDriver +2011-10-29 11:50:19,750 - DEBUG - JdbcUrl=jdbc:hsqldb:mem:hsqldb +2011-10-29 11:50:19,750 - DEBUG - UserName=sa +2011-10-29 11:50:19,750 - DEBUG - Password= +2011-10-29 11:50:19,750 - DEBUG - PasswordCipher=PlainText +2011-10-29 11:50:19,750 - DEBUG - ConnectionProperties= +2011-10-29 11:50:19,750 - DEBUG - DefaultAutoCommit=true +2011-10-29 11:50:19,750 - DEBUG - InitialSize=0 +2011-10-29 11:50:19,750 - DEBUG - MaxActive=20 +2011-10-29 11:50:19,750 - DEBUG - MaxIdle=20 +2011-10-29 11:50:19,751 - DEBUG - MinIdle=0 +2011-10-29 11:50:19,751 - DEBUG - MaxWait=-1 +2011-10-29 11:50:19,751 - DEBUG - TestOnBorrow=true +2011-10-29 11:50:19,751 - DEBUG - TestOnReturn=false +2011-10-29 11:50:19,751 - DEBUG - TestWhileIdle=false +2011-10-29 11:50:19,751 - DEBUG - TimeBetweenEvictionRunsMillis=-1 +2011-10-29 11:50:19,751 - DEBUG - NumTestsPerEvictionRun=3 +2011-10-29 11:50:19,751 - DEBUG - MinEvictableIdleTimeMillis=1800000 +2011-10-29 11:50:19,751 - DEBUG - PoolPreparedStatements=false +2011-10-29 11:50:19,751 - DEBUG - MaxOpenPreparedStatements=0 +2011-10-29 11:50:19,751 - DEBUG - AccessToUnderlyingConnectionAllowed=false +2011-10-29 11:50:19,781 - DEBUG - createService.success +2011-10-29 11:50:19,783 - DEBUG - Containers : 0 +2011-10-29 11:50:19,785 - DEBUG - Deployments : 0 +2011-10-29 11:50:19,785 - DEBUG - SecurityService : org.apache.openejb.core.security.SecurityServiceImpl +2011-10-29 11:50:19,786 - DEBUG - TransactionManager: org.apache.geronimo.transaction.manager.GeronimoTransactionManager +2011-10-29 11:50:19,786 - DEBUG - OpenEJB Container System ready. +2011-10-29 11:50:19,786 - DEBUG - Using default 'openejb.validation.skip=false' +2011-10-29 11:50:19,786 - DEBUG - Using default 'openejb.deploymentId.format={ejbName}' +2011-10-29 11:50:19,786 - DEBUG - Using default 'openejb.debuggable-vm-hackery=false' +2011-10-29 11:50:19,786 - DEBUG - Using default 'openejb.webservices.enabled=true' +2011-10-29 11:50:19,786 - DEBUG - Using default 'openejb.vendor.config=ALL' Possible values are: geronimo, glassfish, jboss, weblogic or NONE or ALL +2011-10-29 11:50:19,789 - DEBUG - Using default 'openejb.deployments.classpath.include=.*' +2011-10-29 11:50:19,789 - DEBUG - Using default 'openejb.deployments.classpath.exclude=' +2011-10-29 11:50:19,789 - DEBUG - Using default 'openejb.deployments.classpath.require.descriptor=client' Possible values are: ejb, client or NONE or ALL +2011-10-29 11:50:19,789 - DEBUG - Using default 'openejb.deployments.classpath.filter.descriptors=false' +2011-10-29 11:50:19,789 - DEBUG - Using default 'openejb.deployments.classpath.filter.systemapps=true' +2011-10-29 11:50:19,828 - DEBUG - Inspecting classpath for applications: 5 urls. +2011-10-29 11:50:19,846 - INFO - Found EjbModule in classpath: /Users/dblevins/examples/troubleshooting/target/classes +2011-10-29 11:50:20,011 - DEBUG - URLs after filtering: 55 +2011-10-29 11:50:20,011 - DEBUG - Annotations path: file:/Users/dblevins/examples/troubleshooting/target/classes/ +2011-10-29 11:50:20,011 - DEBUG - Annotations path: jar:file:/Users/dblevins/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.2/surefire-api-2.7.2.jar!/ +2011-10-29 11:50:20,011 - DEBUG - Annotations path: jar:file:/Users/dblevins/.m2/repository/org/apache/openejb/mbean-annotation-api/4.0.0-beta-1/mbean-annotation-api-4.0.0-beta-1.jar!/ +2011-10-29 11:50:20,011 - DEBUG - Annotations path: jar:file:/Users/dblevins/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.2/surefire-booter-2.7.2.jar!/ +2011-10-29 11:50:20,011 - DEBUG - Annotations path: file:/Users/dblevins/examples/troubleshooting/target/test-classes/ +2011-10-29 11:50:20,011 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/geronimo/specs/geronimo-jms_1.1_spec/1.1.1/geronimo-jms_1.1_spec-1.1.1.jar!/ +2011-10-29 11:50:20,011 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/bval/bval-core/0.3-incubating/bval-core-0.3-incubating.jar!/ +2011-10-29 11:50:20,011 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/geronimo/specs/geronimo-j2ee-management_1.1_spec/1.0.1/geronimo-j2ee-management_1.1_spec-1.0.1.jar!/ +2011-10-29 11:50:20,011 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/activemq/activemq-core/5.4.2/activemq-core-5.4.2.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/xbean/xbean-bundleutils/3.8/xbean-bundleutils-3.8.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/net/sf/scannotation/scannotation/1.0.2/scannotation-1.0.2.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openejb/javaee-api/6.0-2/javaee-api-6.0-2.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-beanutils/commons-beanutils-core/1.8.3/commons-beanutils-core-1.8.3.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/avalon-framework/avalon-framework/4.1.3/avalon-framework-4.1.3.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openwebbeans/openwebbeans-web/1.1.1/openwebbeans-web-1.1.1.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/wsdl4j/wsdl4j/1.6.2/wsdl4j-1.6.2.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/logkit/logkit/1.0.1/logkit-1.0.1.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/com/ibm/icu/icu4j/4.0.1/icu4j-4.0.1.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/xbean/xbean-asm-shaded/3.8/xbean-asm-shaded-3.8.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openwebbeans/openwebbeans-ee-common/1.1.1/openwebbeans-ee-common-1.1.1.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-pool/commons-pool/1.5.6/commons-pool-1.5.6.jar!/ +2011-10-29 11:50:20,012 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-logging/commons-logging-api/1.1/commons-logging-api-1.1.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openwebbeans/openwebbeans-impl/1.1.1/openwebbeans-impl-1.1.1.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/xbean/xbean-finder-shaded/3.8/xbean-finder-shaded-3.8.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/geronimo/specs/geronimo-j2ee-connector_1.6_spec/1.0/geronimo-j2ee-connector_1.6_spec-1.0.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-cli/commons-cli/1.2/commons-cli-1.2.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/activemq/kahadb/5.4.2/kahadb-5.4.2.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/hsqldb/hsqldb/1.8.0.10/hsqldb-1.8.0.10.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/log4j/log4j/1.2.16/log4j-1.2.16.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/geronimo/components/geronimo-connector/3.1.1/geronimo-connector-3.1.1.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/activemq/activemq-ra/5.4.2/activemq-ra-5.4.2.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/net/sourceforge/serp/serp/1.13.1/serp-1.13.1.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/javax/servlet/servlet-api/2.3/servlet-api-2.3.jar!/ +2011-10-29 11:50:20,013 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/activemq/activeio-core/3.1.2/activeio-core-3.1.2.jar!/ +2011-10-29 11:50:20,014 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/quartz-scheduler/quartz/1.8.5/quartz-1.8.5.jar!/ +2011-10-29 11:50:20,014 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openwebbeans/openwebbeans-ee/1.1.1/openwebbeans-ee-1.1.1.jar!/ +2011-10-29 11:50:20,014 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar!/ +2011-10-29 11:50:20,014 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openwebbeans/openwebbeans-spi/1.1.1/openwebbeans-spi-1.1.1.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/codehaus/swizzle/swizzle-stream/1.0.2/swizzle-stream-1.0.2.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openjpa/openjpa/2.1.1/openjpa-2.1.1.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/xbean/xbean-naming/3.8/xbean-naming-3.8.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/geronimo/components/geronimo-transaction/3.1.1/geronimo-transaction-3.1.1.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/javassist/javassist/3.12.0.GA/javassist-3.12.0.GA.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/objectweb/howl/howl/1.0.1-1/howl-1.0.1-1.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/xbean/xbean-reflect/3.8/xbean-reflect-3.8.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/openwebbeans/openwebbeans-ejb/1.1.1/openwebbeans-ejb-1.1.1.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-logging/commons-logging/1.1/commons-logging-1.1.jar!/ +2011-10-29 11:50:20,016 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-net/commons-net/2.0/commons-net-2.0.jar!/ +2011-10-29 11:50:20,017 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/activemq/protobuf/activemq-protobuf/1.1/activemq-protobuf-1.1.jar!/ +2011-10-29 11:50:20,017 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/commons-dbcp/commons-dbcp/1.4/commons-dbcp-1.4.jar!/ +2011-10-29 11:50:20,017 - DEBUG - Descriptors path: jar:file:/Users/dblevins/.m2/repository/org/apache/geronimo/javamail/geronimo-javamail_1.4_mail/1.8.2/geronimo-javamail_1.4_mail-1.8.2.jar!/ +2011-10-29 11:50:20,017 - DEBUG - Searched 5 classpath urls in 80 milliseconds. Average 16 milliseconds per url. +2011-10-29 11:50:20,023 - INFO - Beginning load: /Users/dblevins/examples/troubleshooting/target/classes +2011-10-29 11:50:20,028 - DEBUG - Using default 'openejb.tempclassloader.skip=none' Possible values are: none, annotations, enums or NONE or ALL +2011-10-29 11:50:20,030 - DEBUG - Using default 'openejb.tempclassloader.skip=none' Possible values are: none, annotations, enums or NONE or ALL +2011-10-29 11:50:20,099 - INFO - Configuring enterprise application: /Users/dblevins/examples/troubleshooting +2011-10-29 11:50:20,099 - DEBUG - No ejb-jar.xml found assuming annotated beans present: /Users/dblevins/examples/troubleshooting, module: troubleshooting +2011-10-29 11:50:20,213 - DEBUG - Searching for annotated application exceptions (see OPENEJB-980) +2011-10-29 11:50:20,214 - DEBUG - Searching for annotated application exceptions (see OPENEJB-980) +2011-10-29 11:50:20,248 - WARN - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime. +2011-10-29 11:50:20,249 - DEBUG - looking for annotated MBeans in +2011-10-29 11:50:20,249 - DEBUG - registered 0 annotated MBeans in +2011-10-29 11:50:20,278 - INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +2011-10-29 11:50:20,278 - INFO - Auto-creating a container for bean Movies: Container(type=STATELESS, id=Default Stateless Container) +2011-10-29 11:50:20,278 - INFO - Creating Container(id=Default Stateless Container) +2011-10-29 11:50:20,279 - DEBUG - AccessTimeout=30 seconds +2011-10-29 11:50:20,279 - DEBUG - MaxSize=10 +2011-10-29 11:50:20,279 - DEBUG - MinSize=0 +2011-10-29 11:50:20,279 - DEBUG - StrictPooling=true +2011-10-29 11:50:20,279 - DEBUG - MaxAge=0 hours +2011-10-29 11:50:20,279 - DEBUG - ReplaceAged=true +2011-10-29 11:50:20,279 - DEBUG - ReplaceFlushed=false +2011-10-29 11:50:20,279 - DEBUG - MaxAgeOffset=-1 +2011-10-29 11:50:20,279 - DEBUG - IdleTimeout=0 minutes +2011-10-29 11:50:20,279 - DEBUG - GarbageCollection=false +2011-10-29 11:50:20,279 - DEBUG - SweepInterval=5 minutes +2011-10-29 11:50:20,279 - DEBUG - CallbackThreads=5 +2011-10-29 11:50:20,279 - DEBUG - CloseTimeout=5 minutes +2011-10-29 11:50:20,295 - DEBUG - createService.success +2011-10-29 11:50:20,296 - INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +2011-10-29 11:50:20,296 - INFO - Auto-creating a container for bean org.superbiz.troubleshooting.MoviesTest: Container(type=MANAGED, id=Default Managed Container) +2011-10-29 11:50:20,296 - INFO - Creating Container(id=Default Managed Container) +2011-10-29 11:50:20,310 - DEBUG - createService.success +2011-10-29 11:50:20,310 - INFO - Configuring PersistenceUnit(name=movie-unit) +2011-10-29 11:50:20,310 - DEBUG - raw <jta-data-source>movieDatabase</jta-datasource> +2011-10-29 11:50:20,310 - DEBUG - raw <non-jta-data-source>movieDatabaseUnmanaged</non-jta-datasource> +2011-10-29 11:50:20,310 - DEBUG - normalized <jta-data-source>movieDatabase</jta-datasource> +2011-10-29 11:50:20,310 - DEBUG - normalized <non-jta-data-source>movieDatabaseUnmanaged</non-jta-datasource> +2011-10-29 11:50:20,310 - DEBUG - Available DataSources +2011-10-29 11:50:20,310 - DEBUG - DataSource(name=movieDatabase, JtaManaged=true) +2011-10-29 11:50:20,311 - INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'. +2011-10-29 11:50:20,311 - INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase) +2011-10-29 11:50:20,311 - INFO - Creating Resource(id=movieDatabaseNonJta) +2011-10-29 11:50:20,311 - DEBUG - Definition= +2011-10-29 11:50:20,312 - DEBUG - JtaManaged=false +2011-10-29 11:50:20,312 - DEBUG - JdbcDriver=org.hsqldb.jdbcDriver +2011-10-29 11:50:20,312 - DEBUG - JdbcUrl=jdbc:hsqldb:mem:hsqldb +2011-10-29 11:50:20,312 - DEBUG - UserName=sa +2011-10-29 11:50:20,312 - DEBUG - Password= +2011-10-29 11:50:20,312 - DEBUG - PasswordCipher=PlainText +2011-10-29 11:50:20,312 - DEBUG - ConnectionProperties= +2011-10-29 11:50:20,312 - DEBUG - DefaultAutoCommit=true +2011-10-29 11:50:20,312 - DEBUG - InitialSize=0 +2011-10-29 11:50:20,312 - DEBUG - MaxActive=20 +2011-10-29 11:50:20,312 - DEBUG - MaxIdle=20 +2011-10-29 11:50:20,312 - DEBUG - MinIdle=0 +2011-10-29 11:50:20,312 - DEBUG - MaxWait=-1 +2011-10-29 11:50:20,312 - DEBUG - TestOnBorrow=true +2011-10-29 11:50:20,312 - DEBUG - TestOnReturn=false +2011-10-29 11:50:20,312 - DEBUG - TestWhileIdle=false +2011-10-29 11:50:20,312 - DEBUG - TimeBetweenEvictionRunsMillis=-1 +2011-10-29 11:50:20,312 - DEBUG - NumTestsPerEvictionRun=3 +2011-10-29 11:50:20,312 - DEBUG - MinEvictableIdleTimeMillis=1800000 +2011-10-29 11:50:20,312 - DEBUG - PoolPreparedStatements=false +2011-10-29 11:50:20,312 - DEBUG - MaxOpenPreparedStatements=0 +2011-10-29 11:50:20,312 - DEBUG - AccessToUnderlyingConnectionAllowed=false +2011-10-29 11:50:20,316 - DEBUG - createService.success +2011-10-29 11:50:20,316 - INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged' +2011-10-29 11:50:20,317 - INFO - Using 'openejb.descriptors.output=true' +2011-10-29 11:50:20,317 - INFO - Using 'openejb.descriptors.output=true' +2011-10-29 11:50:20,642 - INFO - Dumping Generated ejb-jar.xml to: /var/folders/bd/f9ntqy1m8xj_fs006s6crtjh0000gn/T/ejb-jar-4107959830671443055troubleshooting.xml +2011-10-29 11:50:20,657 - INFO - Dumping Generated openejb-jar.xml to: /var/folders/bd/f9ntqy1m8xj_fs006s6crtjh0000gn/T/openejb-jar-5369342778223971127troubleshooting.xml +2011-10-29 11:50:20,657 - INFO - Using 'openejb.descriptors.output=true' +2011-10-29 11:50:20,658 - INFO - Dumping Generated ejb-jar.xml to: /var/folders/bd/f9ntqy1m8xj_fs006s6crtjh0000gn/T/ejb-jar-5569422837673302173EjbModule837053032.xml +2011-10-29 11:50:20,659 - INFO - Dumping Generated openejb-jar.xml to: /var/folders/bd/f9ntqy1m8xj_fs006s6crtjh0000gn/T/openejb-jar-560959152015048895EjbModule837053032.xml +2011-10-29 11:50:20,665 - DEBUG - Adding persistence-unit movie-unit property openjpa.Log=log4j +2011-10-29 11:50:20,665 - DEBUG - Adjusting PersistenceUnit(name=movie-unit) property to openjpa.RuntimeUnenhancedClasses=supported +2011-10-29 11:50:20,674 - INFO - Using 'openejb.validation.output.level=VERBOSE' +2011-10-29 11:50:20,674 - INFO - Enterprise application "/Users/dblevins/examples/troubleshooting" loaded. +2011-10-29 11:50:20,674 - INFO - Assembling app: /Users/dblevins/examples/troubleshooting +2011-10-29 11:50:20,678 - DEBUG - Using default 'openejb.tempclassloader.skip=none' Possible values are: none, annotations, enums or NONE or ALL +2011-10-29 11:50:20,757 - DEBUG - Using default 'openejb.tempclassloader.skip=none' Possible values are: none, annotations, enums or NONE or ALL +2011-10-29 11:50:21,137 - INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 407ms +2011-10-29 11:50:21,138 - DEBUG - openjpa.jdbc.SynchronizeMappings=buildSchema(ForeignKeys=true) +2011-10-29 11:50:21,138 - DEBUG - openjpa.Log=log4j +2011-10-29 11:50:21,138 - DEBUG - openjpa.RuntimeUnenhancedClasses=supported +2011-10-29 11:50:21,262 - DEBUG - Using default 'openejb.jndiname.strategy.class=org.apache.openejb.assembler.classic.JndiBuilder$TemplatedStrategy' +2011-10-29 11:50:21,262 - DEBUG - Using default 'openejb.jndiname.format={deploymentId}{interfaceType.annotationName}' +2011-10-29 11:50:21,267 - DEBUG - Using default 'openejb.localcopy=true' +2011-10-29 11:50:21,270 - DEBUG - bound ejb at name: openejb/Deployment/Movies/org.superbiz.troubleshooting.Movies!LocalBean, ref: org.apache.openejb.core.ivm.naming.BusinessLocalBeanReference@2569a1c5 +2011-10-29 11:50:21,270 - DEBUG - bound ejb at name: openejb/Deployment/Movies/org.superbiz.troubleshooting.Movies!LocalBeanHome, ref: org.apache.openejb.core.ivm.naming.BusinessLocalBeanReference@2569a1c5 +2011-10-29 11:50:21,272 - INFO - Jndi(name="java:global/troubleshooting/Movies!org.superbiz.troubleshooting.Movies") +2011-10-29 11:50:21,272 - INFO - Jndi(name="java:global/troubleshooting/Movies") +2011-10-29 11:50:21,277 - DEBUG - Using default 'openejb.jndiname.strategy.class=org.apache.openejb.assembler.classic.JndiBuilder$TemplatedStrategy' +2011-10-29 11:50:21,277 - DEBUG - Using default 'openejb.jndiname.format={deploymentId}{interfaceType.annotationName}' +2011-10-29 11:50:21,277 - DEBUG - bound ejb at name: openejb/Deployment/org.superbiz.troubleshooting.MoviesTest/org.superbiz.troubleshooting.MoviesTest!LocalBean, ref: org.apache.openejb.core.ivm.naming.BusinessLocalBeanReference@3f78e13f +2011-10-29 11:50:21,277 - DEBUG - bound ejb at name: openejb/Deployment/org.superbiz.troubleshooting.MoviesTest/org.superbiz.troubleshooting.MoviesTest!LocalBeanHome, ref: org.apache.openejb.core.ivm.naming.BusinessLocalBeanReference@3f78e13f +2011-10-29 11:50:21,277 - INFO - Jndi(name="java:global/EjbModule837053032/org.superbiz.troubleshooting.MoviesTest!org.superbiz.troubleshooting.MoviesTest") +2011-10-29 11:50:21,277 - INFO - Jndi(name="java:global/EjbModule837053032/org.superbiz.troubleshooting.MoviesTest") +2011-10-29 11:50:21,291 - DEBUG - CDI Service not installed: org.apache.webbeans.spi.ConversationService +2011-10-29 11:50:21,399 - INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +2011-10-29 11:50:21,428 - INFO - Created Ejb(deployment-id=org.superbiz.troubleshooting.MoviesTest, ejb-name=org.superbiz.troubleshooting.MoviesTest, container=Default Managed Container) +2011-10-29 11:50:21,463 - INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateless Container) +2011-10-29 11:50:21,463 - INFO - Started Ejb(deployment-id=org.superbiz.troubleshooting.MoviesTest, ejb-name=org.superbiz.troubleshooting.MoviesTest, container=Default Managed Container) +2011-10-29 11:50:21,463 - INFO - Deployed Application(path=/Users/dblevins/examples/troubleshooting) +2011-10-29 11:50:21,728 - WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@27a8c4e7; ignoring. +2011-10-29 11:50:21,834 - WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@27a8c4e7; ignoring. +2011-10-29 11:50:21,846 - WARN - The class "org.superbiz.testinjection.MoviesTest.Movie" listed in the openjpa.MetaDataFactory configuration property could not be loaded by sun.misc.Launcher$AppClassLoader@27a8c4e7; ignoring. +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.642 sec + +Results : + +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 +---- + + http://git-wip-us.apache.org/repos/asf/tomee-tomee-site-generator/blob/972cc356/src/main/jbake/content/examples/webservice-attachments.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/webservice-attachments.adoc b/src/main/jbake/content/examples/webservice-attachments.adoc new file mode 100755 index 0000000..384f3fd --- /dev/null +++ b/src/main/jbake/content/examples/webservice-attachments.adoc @@ -0,0 +1,234 @@ += Webservice Attachments +:jbake-date: 2016-09-06 +:jbake-type: page +:jbake-tomeepdf: +:jbake-status: published + +Example webservice-attachments can be browsed at https://github.com/apache/tomee/tree/master/examples/webservice-attachments + + +*Help us document this example! Click the blue pencil icon in the upper right to edit this page.* + +== AttachmentImpl + + +[source,java] +---- +package org.superbiz.attachment; + +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.ejb.Stateless; +import javax.jws.WebService; +import javax.xml.ws.BindingType; +import javax.xml.ws.soap.SOAPBinding; +import java.io.IOException; +import java.io.InputStream; + +/** + * This is an EJB 3 style pojo stateless session bean + * Every stateless session bean implementation must be annotated + * using the annotation @Stateless + * This EJB has a single interface: {@link AttachmentWs} a webservice interface. + */ +@Stateless +@WebService( + portName = "AttachmentPort", + serviceName = "AttachmentWsService", + targetNamespace = "http://superbiz.org/wsdl", + endpointInterface = "org.superbiz.attachment.AttachmentWs") +@BindingType(value = SOAPBinding.SOAP12HTTP_MTOM_BINDING) +public class AttachmentImpl implements AttachmentWs { + + public String stringFromBytes(byte[] data) { + return new String(data); + } + + public String stringFromDataSource(DataSource source) { + + try { + InputStream inStr = source.getInputStream(); + int size = inStr.available(); + byte[] data = new byte[size]; + inStr.read(data); + inStr.close(); + return new String(data); + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } + + public String stringFromDataHandler(DataHandler handler) { + + try { + return (String) handler.getContent(); + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } +} +---- + + +== AttachmentWs + + +[source,java] +---- +package org.superbiz.attachment; + +import javax.activation.DataHandler; +import javax.jws.WebService; + +/** + * This is an EJB 3 webservice interface to send attachments throughout SAOP. + */ +@WebService(targetNamespace = "http://superbiz.org/wsdl") +public interface AttachmentWs { + + public String stringFromBytes(byte[] data); + + // Not working at the moment with SUN saaj provider and CXF + //public String stringFromDataSource(DataSource source); + + public String stringFromDataHandler(DataHandler handler); +} +---- + + +== ejb-jar.xml + + +[source,xml] +---- +<ejb-jar/> +---- + + +== AttachmentTest + + +[source,java] +---- +package org.superbiz.attachment; + +import junit.framework.TestCase; + +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.mail.util.ByteArrayDataSource; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.xml.namespace.QName; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.Service; +import javax.xml.ws.soap.SOAPBinding; +import java.net.URL; +import java.util.Properties; + +public class AttachmentTest extends TestCase { + + //START SNIPPET: setup + private InitialContext initialContext; + + protected void setUp() throws Exception { + + Properties properties = new Properties(); + properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); + properties.setProperty("openejb.embedded.remotable", "true"); + + initialContext = new InitialContext(properties); + } + //END SNIPPET: setup + + /** + * Create a webservice client using wsdl url + * + * @throws Exception + */ + //START SNIPPET: webservice + public void testAttachmentViaWsInterface() throws Exception { + Service service = Service.create( + new URL("http://127.0.0.1:4204/AttachmentImpl?wsdl"), + new QName("http://superbiz.org/wsdl", "AttachmentWsService")); + assertNotNull(service); + + AttachmentWs ws = service.getPort(AttachmentWs.class); + + // retrieve the SOAPBinding + SOAPBinding binding = (SOAPBinding) ((BindingProvider) ws).getBinding(); + binding.setMTOMEnabled(true); + + String request = "tszte...@gmail.com"; + + // Byte array + String response = ws.stringFromBytes(request.getBytes()); + assertEquals(request, response); + + // Data Source + DataSource source = new ByteArrayDataSource(request.getBytes(), "text/plain; charset=UTF-8"); + + // not yet supported ! +// response = ws.stringFromDataSource(source); +// assertEquals(request, response); + + // Data Handler + response = ws.stringFromDataHandler(new DataHandler(source)); + assertEquals(request, response); + } + //END SNIPPET: webservice +} +---- + + += Running + + + +[source] +---- +------------------------------------------------------- + T E S T S +------------------------------------------------------- +Running org.superbiz.attachment.AttachmentTest +Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 +http://tomee.apache.org/ +INFO - openejb.home = /Users/dblevins/examples/webservice-attachments +INFO - openejb.base = /Users/dblevins/examples/webservice-attachments +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Found EjbModule in classpath: /Users/dblevins/examples/webservice-attachments/target/classes +INFO - Beginning load: /Users/dblevins/examples/webservice-attachments/target/classes +INFO - Configuring enterprise application: /Users/dblevins/examples/webservice-attachments/classpath.ear +INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) +INFO - Auto-creating a container for bean AttachmentImpl: Container(type=STATELESS, id=Default Stateless Container) +INFO - Enterprise application "/Users/dblevins/examples/webservice-attachments/classpath.ear" loaded. +INFO - Assembling app: /Users/dblevins/examples/webservice-attachments/classpath.ear +INFO - Created Ejb(deployment-id=AttachmentImpl, ejb-name=AttachmentImpl, container=Default Stateless Container) +INFO - Started Ejb(deployment-id=AttachmentImpl, ejb-name=AttachmentImpl, container=Default Stateless Container) +INFO - Deployed Application(path=/Users/dblevins/examples/webservice-attachments/classpath.ear) +INFO - Initializing network services +INFO - Creating ServerService(id=httpejbd) +INFO - Creating ServerService(id=cxf) +INFO - Creating ServerService(id=admin) +INFO - Creating ServerService(id=ejbd) +INFO - Creating ServerService(id=ejbds) +INFO - Initializing network services + ** Starting Services ** + NAME IP PORT + httpejbd 127.0.0.1 4204 + admin thread 127.0.0.1 4200 + ejbd 127.0.0.1 4201 + ejbd 127.0.0.1 4203 +------- +Ready! +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.034 sec + +Results : + +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 +---- + + http://git-wip-us.apache.org/repos/asf/tomee-tomee-site-generator/blob/972cc356/src/main/jbake/content/examples/webservice-handlerchain.adoc ---------------------------------------------------------------------- diff --git a/src/main/jbake/content/examples/webservice-handlerchain.adoc b/src/main/jbake/content/examples/webservice-handlerchain.adoc new file mode 100755 index 0000000..67ac69a --- /dev/null +++ b/src/main/jbake/content/examples/webservice-handlerchain.adoc @@ -0,0 +1,409 @@ += @WebService handlers with @HandlerChain +:jbake-date: 2016-09-06 +:jbake-type: page +:jbake-tomeepdf: +:jbake-status: published + +Example webservice-handlerchain can be browsed at https://github.com/apache/tomee/tree/master/examples/webservice-handlerchain + + +In this example we see a basic JAX-WS `@WebService` component use a handler chain to alter incoming and outgoing SOAP messages. SOAP Handlers are similar to Servlet Filters or EJB/CDI Interceptors. + +At high level, the steps involved are: + + 1. Create handler(s) implementing `javax.xml.ws.handler.soap.SOAPHandler` + 1. Declare and order them in an xml file via `<handler-chain>` + 1. Associate the xml file with an `@WebService` component via `@HandlerChain` + +== The @HandlerChain + +First we'll start with our plain `@WebService` bean, called `Calculator`, which is annotated with `@HandlerChain` + + +[source,java] +---- +@Singleton +@WebService( + portName = "CalculatorPort", + serviceName = "CalculatorService", + targetNamespace = "http://superbiz.org/wsdl", + endpointInterface = "org.superbiz.calculator.wsh.CalculatorWs") +@HandlerChain(file = "handlers.xml") +public class Calculator implements CalculatorWs { + + public int sum(int add1, int add2) { + return add1 + add2; + } + + public int multiply(int mul1, int mul2) { + return mul1 * mul2; + } +} +---- + + +Here we see `@HandlerChain` pointing to a file called `handlers.xml`. This file could be called anything, but it must be in the same jar and java package as our `Calculator` component. + +== The <handler-chains> file + +Our `Calculator` service is in the package `org.superbiz.calculator.wsh`, which means our handler chain xml file must be at `org/superbiz/calculator/wsh/handlers.xml` in our application's classpath or the file will not be found and no handlers will be used. + +In maven we achieve this by putting our handlers.xml in `src/main/resources` like so: + + - `src/main/resources/org/superbiz/calculator/wsh/handlers.xml` + +With this file we declare and **order** our handler chain. + + +[source,xml] +---- +<?xml version="1.0" encoding="UTF-8"?> +<handler-chains xmlns="http://java.sun.com/xml/ns/javaee"> + <handler-chain> + <handler> + <handler-name>org.superbiz.calculator.wsh.Inflate</handler-name> + <handler-class>org.superbiz.calculator.wsh.Inflate</handler-class> + </handler> + <handler> + <handler-name>org.superbiz.calculator.wsh.Increment</handler-name> + <handler-class>org.superbiz.calculator.wsh.Increment</handler-class> + </handler> + </handler-chain> +</handler-chains> +---- + + +The order as you might suspect is: + + - `Inflate` + - `Increment` + +== The SOAPHandler implementation + +Our `Inflate` handler has the job of monitoring *responses* to the `sum` and `multiply` operations and making them 1000 times better. Manipulation of the message is done by walking the `SOAPBody` and editing the nodes. The `handleMessage` method is invoked for both requests and responses, so it is important to check the `SOAPBody` before attempting to naviage the nodes. + + +[source,java] +---- +import org.w3c.dom.Node; +import javax.xml.namespace.QName; +import javax.xml.soap.SOAPBody; +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPMessage; +import javax.xml.ws.handler.MessageContext; +import javax.xml.ws.handler.soap.SOAPHandler; +import javax.xml.ws.handler.soap.SOAPMessageContext; +import java.util.Collections; +import java.util.Set; + +public class Inflate implements SOAPHandler<SOAPMessageContext> { + + public boolean handleMessage(SOAPMessageContext mc) { + try { + final SOAPMessage message = mc.getMessage(); + final SOAPBody body = message.getSOAPBody(); + final String localName = body.getFirstChild().getLocalName(); + + if ("sumResponse".equals(localName) || "multiplyResponse".equals(localName)) { + final Node responseNode = body.getFirstChild(); + final Node returnNode = responseNode.getFirstChild(); + final Node intNode = returnNode.getFirstChild(); + + final int value = new Integer(intNode.getNodeValue()); + intNode.setNodeValue(Integer.toString(value * 1000)); + } + + return true; + } catch (SOAPException e) { + return false; + } + } + + public Set<QName> getHeaders() { + return Collections.emptySet(); + } + + public void close(MessageContext mc) { + } + + public boolean handleFault(SOAPMessageContext mc) { + return true; + } +} +---- + + +The `Increment` handler is identical in code and therefore not shown. Instead of multiplying by 1000, it simply adds 1. + +== The TestCase + +We use the JAX-WS API to create a Java client for our `Calculator` web service and use it to invoke both the `sum` and `multiply` operations. Note the clever use of math to assert both the existence and order of our handlers. If `Inflate` and `Increment` were reversed, the responses would be 11000 and 13000 respectively. + + +[source,java] +---- +public class CalculatorTest { + + @BeforeClass + public static void setUp() throws Exception { + Properties properties = new Properties(); + properties.setProperty("openejb.embedded.remotable", "true"); + EJBContainer.createEJBContainer(properties); + } + + @Test + public void testCalculatorViaWsInterface() throws Exception { + final Service calculatorService = Service.create( + new URL("http://127.0.0.1:4204/Calculator?wsdl"), + new QName("http://superbiz.org/wsdl", "CalculatorService")); + + assertNotNull(calculatorService); + + final CalculatorWs calculator = calculatorService.getPort(CalculatorWs.class); + + // we expect our answers to come back 1000 times better, plus one! + assertEquals(10001, calculator.sum(4, 6)); + assertEquals(12001, calculator.multiply(3, 4)); + } +} +---- + + +== Running the example + +Simply run `mvn clean install` and you should see output similar to the following: + + +[source] +---- +------------------------------------------------------- + T E S T S +------------------------------------------------------- +Running org.superbiz.calculator.wsh.CalculatorTest +INFO - openejb.home = /Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers +INFO - openejb.base = /Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers +INFO - Using 'javax.ejb.embeddable.EJBContainer=true' +INFO - Cannot find the configuration file [conf/openejb.xml]. Will attempt to create one for the beans deployed. +INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) +INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) +INFO - Creating TransactionManager(id=Default Transaction Manager) +INFO - Creating SecurityService(id=Default Security Service) +INFO - Beginning load: /Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers/target/test-classes +INFO - Beginning load: /Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers/target/classes +INFO - Configuring enterprise application: /Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers +INFO - Auto-deploying ejb Calculator: EjbDeployment(deployment-id=Calculator) +INFO - Configuring Service(id=Default Singleton Container, type=Container, provider-id=Default Singleton Container) +INFO - Auto-creating a container for bean Calculator: Container(type=SINGLETON, id=Default Singleton Container) +INFO - Creating Container(id=Default Singleton Container) +INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) +INFO - Auto-creating a container for bean org.superbiz.calculator.wsh.CalculatorTest: Container(type=MANAGED, id=Default Managed Container) +INFO - Creating Container(id=Default Managed Container) +INFO - Enterprise application "/Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers" loaded. +INFO - Assembling app: /Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers +INFO - Created Ejb(deployment-id=Calculator, ejb-name=Calculator, container=Default Singleton Container) +INFO - Started Ejb(deployment-id=Calculator, ejb-name=Calculator, container=Default Singleton Container) +INFO - Deployed Application(path=/Users/dblevins/work/all/trunk/openejb/examples/webservice-handlers) +INFO - Initializing network services +INFO - Creating ServerService(id=httpejbd) +INFO - Creating ServerService(id=cxf) +INFO - Creating ServerService(id=admin) +INFO - Creating ServerService(id=ejbd) +INFO - Creating ServerService(id=ejbds) +INFO - Initializing network services +INFO - ** Starting Services ** +INFO - NAME IP PORT +INFO - httpejbd 127.0.0.1 4204 +INFO - Creating Service {http://superbiz.org/wsdl}CalculatorService from class org.superbiz.calculator.wsh.CalculatorWs +INFO - Setting the server's publish address to be http://nopath:80 +INFO - Webservice(wsdl=http://127.0.0.1:4204/Calculator, qname={http://superbiz.org/wsdl}CalculatorService) --> Ejb(id=Calculator) +INFO - admin thread 127.0.0.1 4200 +INFO - ejbd 127.0.0.1 4201 +INFO - ejbd 127.0.0.1 4203 +INFO - ------- +INFO - Ready! +INFO - Creating Service {http://superbiz.org/wsdl}CalculatorService from WSDL: http://127.0.0.1:4204/Calculator?wsdl +INFO - Creating Service {http://superbiz.org/wsdl}CalculatorService from WSDL: http://127.0.0.1:4204/Calculator?wsdl +INFO - Default SAAJ universe not set +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.783 sec + +Results : + +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 +---- + + +== Inspecting the messages + +The above would generate the following messages. + +=== Calculator wsdl + + +[source,xml] +---- +<?xml version="1.0" encoding="UTF-8"?> +<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" + name="CalculatorService" targetNamespace="http://superbiz.org/wsdl" + xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" + xmlns:tns="http://superbiz.org/wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + <wsdl:types> + <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" + targetNamespace="http://superbiz.org/wsdl" xmlns:tns="http://superbiz.org/wsdl" + xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + <xsd:element name="multiply" type="tns:multiply"/> + <xsd:complexType name="multiply"> + <xsd:sequence> + <xsd:element name="arg0" type="xsd:int"/> + <xsd:element name="arg1" type="xsd:int"/> + </xsd:sequence> + </xsd:complexType> + <xsd:element name="multiplyResponse" type="tns:multiplyResponse"/> + <xsd:complexType name="multiplyResponse"> + <xsd:sequence> + <xsd:element name="return" type="xsd:int"/> + </xsd:sequence> + </xsd:complexType> + <xsd:element name="sum" type="tns:sum"/> + <xsd:complexType name="sum"> + <xsd:sequence> + <xsd:element name="arg0" type="xsd:int"/> + <xsd:element name="arg1" type="xsd:int"/> + </xsd:sequence> + </xsd:complexType> + <xsd:element name="sumResponse" type="tns:sumResponse"/> + <xsd:complexType name="sumResponse"> + <xsd:sequence> + <xsd:element name="return" type="xsd:int"/> + </xsd:sequence> + </xsd:complexType> + </xsd:schema> + </wsdl:types> + <wsdl:message name="multiplyResponse"> + <wsdl:part element="tns:multiplyResponse" name="parameters"> + </wsdl:part> + </wsdl:message> + <wsdl:message name="sumResponse"> + <wsdl:part element="tns:sumResponse" name="parameters"> + </wsdl:part> + </wsdl:message> + <wsdl:message name="sum"> + <wsdl:part element="tns:sum" name="parameters"> + </wsdl:part> + </wsdl:message> + <wsdl:message name="multiply"> + <wsdl:part element="tns:multiply" name="parameters"> + </wsdl:part> + </wsdl:message> + <wsdl:portType name="CalculatorWs"> + <wsdl:operation name="multiply"> + <wsdl:input message="tns:multiply" name="multiply"> + </wsdl:input> + <wsdl:output message="tns:multiplyResponse" name="multiplyResponse"> + </wsdl:output> + </wsdl:operation> + <wsdl:operation name="sum"> + <wsdl:input message="tns:sum" name="sum"> + </wsdl:input> + <wsdl:output message="tns:sumResponse" name="sumResponse"> + </wsdl:output> + </wsdl:operation> + </wsdl:portType> + <wsdl:binding name="CalculatorServiceSoapBinding" type="tns:CalculatorWs"> + <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> + <wsdl:operation name="multiply"> + <soap:operation soapAction="" style="document"/> + <wsdl:input name="multiply"> + <soap:body use="literal"/> + </wsdl:input> + <wsdl:output name="multiplyResponse"> + <soap:body use="literal"/> + </wsdl:output> + </wsdl:operation> + <wsdl:operation name="sum"> + <soap:operation soapAction="" style="document"/> + <wsdl:input name="sum"> + <soap:body use="literal"/> + </wsdl:input> + <wsdl:output name="sumResponse"> + <soap:body use="literal"/> + </wsdl:output> + </wsdl:operation> + </wsdl:binding> + <wsdl:service name="CalculatorService"> + <wsdl:port binding="tns:CalculatorServiceSoapBinding" name="CalculatorPort"> + <soap:address location="http://127.0.0.1:4204/Calculator?wsdl"/> + </wsdl:port> + </wsdl:service> +</wsdl:definitions> +---- + + +=== SOAP sum and sumResponse + +Request: + + +[source,xml] +---- +<?xml version="1.0" encoding="UTF-8"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> + <soap:Body> + <ns1:sum xmlns:ns1="http://superbiz.org/wsdl"> + <arg0>4</arg0> + <arg1>6</arg1> + </ns1:sum> + </soap:Body> +</soap:Envelope> +---- + + +Response: + + +[source,xml] +---- +<?xml version="1.0" encoding="UTF-8"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> + <soap:Body> + <ns1:sumResponse xmlns:ns1="http://superbiz.org/wsdl"> + <return>10001</return> + </ns1:sumResponse> + </soap:Body> +</soap:Envelope> +---- + + +=== SOAP multiply and multiplyResponse + +Request: + + +[source,xml] +---- +<?xml version="1.0" encoding="UTF-8"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> + <soap:Body> + <ns1:multiply xmlns:ns1="http://superbiz.org/wsdl"> + <arg0>3</arg0> + <arg1>4</arg1> + </ns1:multiply> + </soap:Body> +</soap:Envelope> +---- + + +Response: + + +[source,xml] +---- +<?xml version="1.0" encoding="UTF-8"?> +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> + <soap:Body> + <ns1:multiplyResponse xmlns:ns1="http://superbiz.org/wsdl"> + <return>12001</return> + </ns1:multiplyResponse> + </soap:Body> +</soap:Envelope> +---- +