Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling an EJB inside EJB3

 
Kingsley Mullers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have constructed an EJB which when you pass xml data to it will decompose the xml into java objects that you can write to the database but there�s another EJB that I need to call before that which basically calls the XML data or return the XML data.

What I basically need to do here, is to first call the EJB which will return an XML data then read/ or map the XML data to java objects and write to the database.
Could anybody give me a sample code how I would need to do this.
i really appreciate it
here is what i have created.

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import java.io.File;
import java.util.*;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;
import za.co.fnb.pyramid.exceptions.ProcessEngineException;
import za.co.fnb.pyramid.interfaces.ProcessEngineSessionFacade;


@Stateless(name="ReadingXMLDataSessionFacadeBean", mappedName="ejb/ReadingXMLDataSessionFacade")

public class ReadingXMLDataSessionFacadeBean implements ProcessEngineSessionFacade {

private static final Logger LOG = Logger.getLogger( ReadingXMLDataSessionFacadeBean.class );
public ReadingXMLDataSessionFacadeBean() {
super();
LOG.debug( "ReadingXMLDataSessionFacadeBean()" );

}

public void ping()throws ProcessEngineException, RemoteException {
LOG.debug( "ping()" );
//LOG.debug( "processData(String xmlData)" );
{
try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false); //do not validate the xml
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource("Contacts.xml"));
LOG.debug("doc");
//get contact list
NodeList list = doc.getChildNodes().item(0).getChildNodes();
for (int i = 0; i < list.getLength(); i++){
if (list.item(i).getNodeName().equalsIgnoreCase("contact")){



NodeList contact = list.item(i).getChildNodes();
LOG.debug("contact");
//build contact object
StringBuffer contactObject = new StringBuffer();
for (int j = 0; j < contact.getLength(); j++){
if (!contact.item(j).getNodeName().equalsIgnoreCase("#text")){
if (j > 1) {contactObject.append(",");}
contactObject.append(contact.item(j).getNodeName()+"="+contact.item(j).getFirstChild().toString());

}

}
//write contact object to database
System.out.println(contactObject.toString());
}

}

}
catch (Exception ex){
ex.printStackTrace();

}

}

/** public static void main(String[] args) {
new InputData().processData("");
}

}**/

}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you are deploying in an EJB3 container you should be able to simply inject the other EJB.

so an Attribute of your class like

@EJB
MyOtherEJBClass otherEjb;




Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic