Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Java EE 6: How to add web module on top of application client
- public static void main(String[] args) {
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("CoreInPU");
- EntityManager em = emf.createEntityManager();
- EntityDAO entityDAOClient = new EntityDAOClient(em);
- Main pgm = new Main();
- try {
- process(entityDAOClient);
- } catch (Exception e) {
- logger.fatal("", e);
- }finally{
- em.close();
- emf.close();
- }
- }
- public void process(EntityDAO entityDAO){
- validatePDF(List<pdfFiles>);
- processPDF(List<pdfFiles>, entityDAO);
- createPrintJob(List<pdfFiles>, entityDAO);
- }
- public void processPDF(List<pdfFiles>, EntityDAO entityDAO){
- for(File file : pdfFiles){
- entityDAO.create(file);
- }
- }
- public interface EntityDAO {
- public <T> T create(T t);
- public <T> T find(Class<T> type, Object id);
- public List findWithNamedQuery(String queryName);
- public List findWithNamedQuery(String queryName, int resultLimit);
- }
- public class EntityDAOClient implements EntityDAO {
- private EntityManager em;
- private static Logger logger = Logger.getLogger(EntityDAOClient.class);
- public EntityDAOClient(EntityManager em) {
- this.em = em;
- }
- @Override
- public <T> T create(T t){
- em.getTransaction().begin();
- em.persist(t);
- em.getTransaction().commit();
- return t;
- }
- @Override
- public <T> T find(Class<T> type, Object id){
- em.getTransaction().begin();
- T t = em.find(type, id);
- em.getTransaction().commit();
- return t;
- }
- ...
- }
- <persistence-unit name="CoreInPU" transaction-type="RESOURCE_LOCAL">
- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>com.wf.docsys.core.entity.Acknowledgement</class>
- <class>com.wf.docsys.core.entity.PackageLog</class>
- <properties>
- <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/core"/>
- <property name="javax.persistence.jdbc.password" value="root"/>
- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
- <property name="javax.persistence.jdbc.user" value="xxxx"/>
- <property name="eclipselink.ddl-generation" value="create-tables"/>
- </properties>
- </persistence-unit>
- @Stateless
- @LocalBean
- public class CoreEJB implements EntityDAO{
- @PersistenceContext(unitName = "CoreInWeb-ejbPU")
- private EntityManager em;
- //@Override
- public <T> T create(T t) {
- em.persist(t);
- return t;
- }
- //@Override
- public <T> T find(Class<T> type, Object id) {
- return em.find(type, id);
- }
- ...
- }
- @ManagedBean
- @RequestScoped
- public class Bean {
- @EJB
- private CoreEJB coreEJB;
- public Bean() {
- }
- public void runAppClientMainProcess() {
- //The web interface can also kick off the same process as the app client
- process(coreEJB);
- }
- // ...
- }
- @Stateless
- public class SomeEJB {
- @PersistenceUnit(unitName="someWebPU")
- private EntityManagerFactory emf;
- @PostConstruct
- public void init() {
- EntityUtil.newInstance(emf);
- }
- public void create(Some some) {
- EntityUtil.create(some);
- }
- // ...
- }
- public interface SomeDAO {
- public void save(Some some);
- // ...
- }
- public class SomeDAOClient implements SomeDAO {
- private EntityManager em;
- public SomeDAO(EntityManager em) {
- this.em = em;
- }
- public void save(Some some) {
- em.getTransaction().begin();
- em.persist(some);
- em.getTransaction().commit();
- }
- // ...
- }
- public static void main(String[] args) throws Exception {
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("someClientPU");
- EntityManager em = emf.createEntityManager();
- SomeDAO someDAO = new SomeDAOClient(em);
- try {
- Some some = new Some();
- some.setFoo("foo");
- someDAO.save(some);
- } finally {
- em.close();
- emf.close();
- }
- }
- @Stateless
- public class SomeDAOEJB implements SomeDAO {
- @PersistenceContext(unitName="someWebPU")
- private EntityManager em;
- public void save(Some some) {
- em.persist(some);
- }
- // ...
- }
- @ManagedBean
- @RequestScoped
- public class Bean {
- @EJB
- private SomeDAO someDAO;
- private Some some;
- public Bean() {
- some = new Some();
- }
- public void save() {
- someDAO.save(some);
- }
- // ...
- }
- //This class works as is in both javaEE and client.
- //There is no need of managedbean in CDI.
- //This class can be accessed in jsf using EL #businessProcessor
- @Singleton @Named
- public class BusinessProcessor
- {
- private @Inject EntityDAO entityDAO; // CDI will inject an implementation of EntityDao.
- //In our case there is only one impl for client as well as JavaEE
- // CDI will invoke below method on startup. Not used in JavaEE.
- // In JavaEE JSF will execute process() directly using Expression Language
- public void init(@Observes ContainerInitialized event, @Parameters List parameters){
- process(entityDAO);
- }
- public void process(@Inject EntityDAO entityDAO){
- validatePDF(List);
- processPDF(List, entityDAO);
- createPrintJob(List, entityDAO);
- }
- public void processPDF(List, EntityDAO entityDAO){
- for(File file : pdfFiles){
- entityDAO.create(file);
- }
- }
- }
- // this class is same for both JavaEE and Client
- public class EntityDAOClient implements EntityDAO {
- private @Inject EntityManager em;
- private static Logger logger = Logger.getLogger(EntityDAOClient.class);
- @Override
- public T create(T t){
- em.getTransaction().begin();
- em.persist(t);
- em.getTransaction().commit();
- return t;
- }
- @Override
- public T find(Class type, Object id){
- em.getTransaction().begin();
- T t = em.find(type, id);
- em.getTransaction().commit();
- return t;
- }
- ...
- }
- java org.jboss.weld.environment.se.StartMain
- public class Main {
- @EJB
- private static CoreMainEJBRemote coreEJBRemote;
- private static Logger logger = Logger.getLogger(Main.class);
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- coreEJBRemote.process(args[0]);
- }
- }
- @Stateless
- public class CoreMainEJB implements CoreMainEJBRemote, CoreMainEJBLocal {
- @EJB
- private PackageProcessor packageProcessor;
- @Override
- public void process(String configFileName) {
- ...
- //Process config File
- packageProcessor.validatePDF();
- packageProcessor.processPDF();
- ...
- }
- }
Add Comment
Please, Sign In to add comment