-
Simple Bean Examples
hi guys,
I'm just starting to get into all the j2ee stuff. Recently I've gone over a couple examples with EJBs. I'm wondering if any of you have any more simple EJBs that I could check out. (probably the first ones you wrote!)
I learn by example very well, and most of the EJB samples I've found are little much to start with. Anything you can hook me up with would be great! Send it however you like.
-
the one that i am posting is an example of Message driven bean. needs JMS......
i used weblogic8.1
let me post the web.xml, weblogic-ejb-jar.xml,EmailMDB.java and EmailClient.java
hope that helps
web.xml.....
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>EmailMDB</ejb-name>
<ejb-class>EmailMDB</ejb-class>
<transaction-type>Container</transaction-type>
<acknowledge-mode>Auto-acknowledge</acknowledge-mode>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
<subscription-durability>Durable</subscription-durability>
</message-driven-destination>
</message-driven>
</enterprise-beans>
</ejb-jar>
weblogic-ejb-jar.xml
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN" "http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd" >
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>EmailMDB</ejb-name>
<message-driven-descriptor>
<pool>
<max-beans-in-free-pool>200</max-beans-in-free-pool>
<initial-beans-in-free-pool>5</initial-beans-in-free-pool>
</pool>
<destination-jndi-name>myqueue</destination-jndi-name>
</message-driven-descriptor>
<jndi-name>jms/EmailMDB</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
EmailMDB.java
import java.util.*;
import javax.ejb.*;
import javax.jms.*;
public class EmailMDB implements MessageDrivenBean, MessageListener {
private MessageDrivenContext context;
// --------------------------------------------------------------
// EJB Methods from MessageDrivenBean interface
// --------------------------------------------------------------
public void ejbCreate() {
System.out.println("EmailMDB: ejbCreate called");
}
public void ejbRemove() {
System.out.println("EmailMDB: ejbRemove called");
}
public void setMessageDrivenContext(MessageDrivenContext context) {
System.out.println("setMessageDrivenContext called");
this.context = context;
}
public void onMessage(Message message) {
System.out.println("EmailMDB: onMessage called");
TextMessage mapmessage = (TextMessage) message;
try {
// Go through the map message and create a map for sendmail(Map)
System.out.println("Sending email: EmailHelper.sendmail(mail)" + mapmessage.getText());
} catch(Exception e) {
e.printStackTrace();
}
}
}
EmailClient.java
import java.rmi.*;
import java.util.*;
import javax.jms.*;
import javax.naming.*;
/**
* EmailClient is responsible for sending an email mapmessage to the queue
*/
public class EmailClient {
private static String QUEUE_NAME = "myqueue";
private static String CONNECTION_FACTORY = "javax.jms.QueueConnectionFactory";
private static String PROVIDER_URL = "t3://localhost:7001";
private QueueSender sender;
private QueueSession session;
/**
* Constructor: Setup JMS for publishing
*/
public EmailClient() {
try {
Context ctx = getInitialContext();
// Lookup a JMS connection factory
QueueConnectionFactory conFactory =
(QueueConnectionFactory) ctx.lookup(CONNECTION_FACTORY);
// Create a JMS connection
QueueConnection connection = conFactory.createQueueConnection();
// Create a JMS session object
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Lookup a JMS queue
Queue chatQueue = (Queue) ctx.lookup(QUEUE_NAME);
// Create a JMS sender
sender = session.createSender(chatQueue);
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Method: sendmail(Map mail)
*/
public void sendmail() throws Exception {
TextMessage message = session.createTextMessage();
message.setText("this is working");
// send the mapmessage to the Queue
sender.send(message);
}
/**
* Method: getInitialContext()
*
* Login to JNDI
*
* @return Context The initial context
*/
private Context getInitialContext() throws NamingException {
Properties env = new Properties();
env.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
env.put("java.naming.provider.url", PROVIDER_URL);
return new InitialContext(env);
}
/**
* Static Method: java com.customware.client.EmailClient to_addr [from_addr] [subject] [body]
*/
public static void main(String args[]) throws Exception {
System.out.println("\nBeginning EmailClient\n");
EmailClient client = new EmailClient();
System.out.println("Sending mail message");
client.sendmail();
}
}
all the best
Kandati
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks