Friday, January 25, 2008


Sending a notification through an e-mail with EJB



The ability to send email is an important part of many enterprise applications. E-mail can be used to send notifications, alerts, and general information, such as price quotes or contract information. When sending an email from an EJB, you should be able to continue processing without waiting for an email to be sent.

Combining email-sending code with a message-driven bean provides the asynchronous behavior that is ideal for enterprise applications.

This Java tip contains a message-driven bean that sends email using property values passed to it via a JMS message:


public class NotifierBean implements MessageDrivenBean, MessageListener {
    
    private MessageDrivenContext _context;
    
    ...
    
    public void onMessage(Message msg) {
        
        MapMessage map = (MapMessagemsg;
        
        try {
            // simple forward to E-mail:
            sendEmail(map.getProperty("recipient"),
                    map.getProperty("message"));
            
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void sendEmail(String recipient, String textthrows Exception {
        
        try {
            
            Session session = (Session_context.lookup("AdminSession");
            
            Message message = new MimeMessage(session);
            message.setFrom();
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipient, false));
            
            message.setSubject("Notification");
            message.setText(text);
            
            // sends the email message:
            Transport.send(message);
            
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    ...
}

Tuesday, January 22, 2008

Error: javax.naming. ..... Bean not bound.

<ejb-name> tag does not define the JNDI name of your bean. It is just a logical name given to your EJB.



Assuming in your file ejb-jar.xml you have defined your bean with the logical name called SectionBean (using the tag <ejb-name>SectionBean</ejb-name>) , you need to provide other information in another file called jboss.xml (the file name and contents is EJB container specific. With JBoss , it is jboss.xml . With Weblogic , it is weblogic-ejb-jar.xml)



In your jboss.xml is the place where you give the JNDI name for your bean. Look at the example below. That is roughly what you should have defined in jboss.xml



<jboss>

     <secure>true</secure>

     <container-configurations />

     <resource-managers />

     <enterprise-beans>

       <entity>

<ejb-name>SectionBean</ejb-name>

<jndi-name>SectionBean</jndi-name>

<configuration-name></configuration-name>

       </entity>

     </enterprise-beans>

</jboss>



Note that you should provide the EJB's logical name here as well (within the tag <ejb-name>). The logical name should map to what you have defined in ejb-jar.xml . The JNDI name is given within the <jndi-name> tag. You can change it to ,let's say <jndi-name>FooBean</jndi-name>. If you do that , you can look up the bean using the JNDI name "FooBean"