Vous n'êtes pas connecté. Connexion
|
|
Weblogic / Java EE6De $1IntroductionThis page is a good reminder for the Java EE6 developper that plan to use Eclipse indigo with weblogic 12c or greater for developping Servlets/JSPs/EJB/JMS applications. Installing Java DB + sample database + DataSource in Eclipse + in WeblogicInstall and Launch JavaDB
Add the connection to the DB admin tool in Eclipse java EE perspective
Add the DataSource to Weblogic 12cIn WebLogic 12c, run the admin server from the windows start menu, then open the weblogic admin console (also from start menu).
Writing a remote client standalone for an EJBYou must have at least one EJB that implements a remote interface. Example : package sessions; import javax.ejb.LocalBean; import javax.ejb.Stateless; /** * Session Bean implementation class Test */ @Stateless @LocalBean public class DemoSessionEJBRemote implements TestRemote { public DemoSessionEJBRemote() { // TODO Auto-generated constructor stub } public String helloWorld() { return "hello world !"; }} And here is the remote interface (located in the Client project that is automatically created when we do an EAR project): package sessions; import javax.ejb.Remote; @Remote public interface TestRemote { public String helloWorld(); } The client project is just a Java project, but you must add two things so that it compiles and run:
Then you should have such a source code for the standalone client: package client; import java.util.Hashtable;import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import sessions.TestRemote; public class StandAloneClient { private static String INITIAL_CONTEXT_NAME = "weblogic.jndi.WLInitialContextFactory"; private static String PROVIDER_URL = "t3://localhost:7001"; private static String JNDI_NAME = "java:global.DemoEAR.DemoEJB.DemoSessionEJBRemote!sessions.TestRemote"; public static void main(String[] args) throws Exception { Context ctx = getInitialContext(); TestRemote helloEJB = (TestRemote) ctx.lookup(JNDI_NAME); System.out.println(helloEJB.helloWorld()); } private static Context getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_NAME); env.put(Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext(env); } } Use JMS and a Message Driven Bean on WeblogicInstalling a JMS queue on WebLogicGood guide on how to setup a JMS queue here : http://blog.touret.info/blog/index.php/post/2011/09/22/Mise-en-oeuvre-d-une-file-d-attente-JMS-sur-Weblogic-10.3 (in french) Writing a message driven beanNote: Message Driven Beans are not supported by the JavaEE 6 "Web profile" (all in one projects with jsf/jsp/servlets/ejb/jpa in one single project, deployed as a single war file). For MDB you need to create an EAR project, and an EJB project (maybe with JPA facet), and maybe a Dynamic Web project (for jsf/jsp/servlets). The MDB should be added into the EJB project. The client servlet we demonstrate here should be added in the dynamic web project. Normally the Eclipse wizard should ask you for the names of the connexion factory + destination. Here is an example of a MDB that works with the client from the next section.
Example MDB: package sessions;import javax.ejb.ActivationConfigProperty;import javax.ejb.MessageDriven;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;@MessageDriven( activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue" ) }, mappedName = "weblogic.jms.DemoQueue")public class TestMDB implements MessageListener { public TestMDB() { // TODO Auto-generated constructor stub System.out.println("TestMDB : NEW INSTANCE BUILT !"); } public void onMessage(Message message) { if(message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; try { String msg = txtMsg.getText(); System.out.println("Message received: " + msg); System.out.println("Processing message"); Thread.sleep(5000); System.out.println("End of processing message"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} package sessions;import javax.ejb.ActivationConfigProperty;import javax.ejb.MessageDriven;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;@MessageDriven( activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue" ) }, mappedName = "weblogic.jms.DemoQueue")public class TestMDB implements MessageListener { public TestMDB() { // TODO Auto-generated constructor stub System.out.println("TestMDB : NEW INSTANCE BUILT !"); } public void onMessage(Message message) { if(message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; try { String msg = txtMsg.getText(); System.out.println("Message received: " + msg); System.out.println("Processing message"); Thread.sleep(5000); System.out.println("End of processing message"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} Writing a client for a weblogic queueHere is an example of a servlet that sends messages to the previous message driven bean. In fact it doesn't know the actual receiver of the message, it just knows the destination where the message is being sent. Again, double check the names of the connection factory and of the destination. They should exactly match the ones in the weblogic admin console and the ones in the MDB. package servlets;
|
Powered by MindTouch Deki Open Source Edition v.8.08 |