Hello Everyone,
I'm a new to JavaEE and currently I'm following the The Java EE 6 Tutorial
(March 2011).
System:
- Windows 7 Professional 32-bit
- java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing)
- GlassFish Server 3.1 Open Source glassfish-3.1-windows.exe
- netbeans-6.9.1-ml-windows.exe
- apache-maven-3.0.2-bin.zip
After installations are done, I used Update Tool to obtain the Java EE
Tutorial and it is now at C:\glassfish3.1\glassfish\docs\javaee-tutorial\
where C:\glassfish3.1\ is the installed_dir of the GlassFish Server 3.1 Open
Source.
Then now I'm following the The Java EE 6 Tutorial that is
C:\glassfish3.1\glassfish\docs\javaee-tutorial\doc\javaeetutorial6.pdf.
Now I'm building, deploying, and running the example async that is one of EJB
examples, located at
C:\glassfish3.1\glassfish\docs\javaee-tutorial\examples\ejb\async\.
It was builded OK.
It was deployed OK.
At the browser Firefox 3.6.16, I went to
http://localhost:8080/async/. I
provided an email and then clicked Send email button, I saw an error below:
javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException:
Could not create stateless EJB
The server.log (in C:\glassfish3.1\glassfish\domains\domain1\logs\) showed:
[#|2011-03-30T09:51:39.397-0700|INFO|glassfish3.1|org.hibernate.validator.engine.resolver.DefaultTraversableResolver|_ThreadID=29;_ThreadName=Thread-1;|Instantiated
an instance of
org.hibernate.validator.engine.resolver.JPATraversableResolver.|#]
[#|2011-03-30T09:51:39.464-0700|SEVERE|glassfish3.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=180;_ThreadName=Thread-1;|EJB5070:
Exception creating stateless session bean : [MailerBean]|#]
[#|2011-03-30T09:51:39.466-0700|WARNING|glassfish3.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=180;_ThreadName=Thread-1;|A
system exception occurred during an invocation on EJB MailerBean method
public java.util.concurrent.Future
async.ejb.MailerBean.sendMessage(java.lang.String)
javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException:
Could not create stateless EJB
at
com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
at
com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2528)
at
com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1895)
at com.sun.ejb.containers.EjbAsyncTask.call(EjbAsyncTask.java:99)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not
create stateless EJB
at
com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
at
com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
at
com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
... 8 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
at
com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
at
com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
at
com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
... 10 more
Caused by: java.lang.IllegalStateException: Exception attempting to inject
Res-Ref-Env-Property: mail/myExampleSession_at_javax.mail.Session@ resolved as:
jndi: mail/myExampleSession_at_res principal: null_at_mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class async.ejb.MailerBean
at
org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:137)
at
org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:50)
at org.jboss.weld.bean.SessionBean$1.inject(SessionBean.java:190)
at
org.glassfish.weld.services.JCDIServiceImpl.injectEJBInstance(JCDIServiceImpl.java:198)
at
com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1675)
at
com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:494)
... 12 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException:
Exception attempting to inject Res-Ref-Env-Property:
mail/myExampleSession_at_javax.mail.Session@ resolved as: jndi:
mail/myExampleSession_at_res principal: null_at_mail: null
No Runtime properties
Database Vendor : null
Create Tables at Deploy : false
Delete Tables at Undeploy : false into class async.ejb.MailerBean
at
com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:698)
at
com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:468)
at
com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:173)
at
org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:130)
... 17 more
Caused by: javax.naming.NamingException: Lookup failed for
'java:comp/env/mail/myExampleSession' in
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is
javax.naming.NamingException: Lookup failed for 'mail/myExampleSession' in
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming,
com.sun.enterprise.naming.logicalName=java:comp/env/mail/myExampleSession}
[Root exception is javax.naming.NameNotFoundException: mail]]
at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at
com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:597)
... 20 more
Caused by: javax.naming.NamingException: Lookup failed for
'mail/myExampleSession' in
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming,
com.sun.enterprise.naming.logicalName=java:comp/env/mail/myExampleSession}
[Root exception is javax.naming.NameNotFoundException: mail]
at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at
com.sun.enterprise.naming.util.JndiNamingObjectFactory.create(JndiNamingObjectFactory.java:90)
at
com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:772)
at
com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:740)
at
com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:172)
at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
... 24 more
Caused by: javax.naming.NameNotFoundException: mail
at
com.sun.enterprise.naming.impl.TransientContext.resolveContext(TransientContext.java:310)
at
com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:218)
at
com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
at
com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)
at
com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)
... 32 more
|#]
I put the entire project async in a zip file but this forum does not provide
the way to attach a file. So below is the source:
*glassfish-resources.xml*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application
Server 3.1 Resource Definitions//EN"
"
http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<mail-resource debug="false"
enabled="true"
*from*="test_at_gmail.com"
*host*="smtp.googlemail.com"
jndi-name="mail/myExampleSession"
object-type="user" store-protocol="imap"
store-protocol-class="com.sun.mail.imap.IMAPStore"
transport-protocol="smtps"
transport-protocol-class="com.sun.mail.smtp.SMTPSSLTransport"
*user*="test_at_gmail.com">
<description/>
<property name="mail-smtps-auth" value="true"/>
<property name="*mail-smtps-password*" value="mypassword"/>
</mail-resource>
</resources>
You need to change the from, host, user, and mail-smtps-password to your
info.
*MailerBean.java*
/*
* Copyright 2011 Oracle and/or its affiliates.
* All rights reserved. You may not modify, use,
* reproduce, or distribute this software except in
* compliance with the terms of the License at:
*
http://developers.sun.com/license/berkeley_license.html
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package async.ejb;
import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author ievans
*/
@Named
@Stateless
public class MailerBean {
private static final Logger logger =
Logger.getLogger("async.ejb.MailerBean");
@Resource(name = "mail/myExampleSession")
private Session session;
@Asynchronous
public Future<String> sendMessage(String email) {
String status;
try {
Message message = new MimeMessage(session);
message.setFrom();
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email, false));
message.setSubject("Test message from async example");
message.setHeader("X-Mailer", "JavaMail");
DateFormat dateFormatter =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
Date timeStamp = new Date();
String messageBody = "This is a test message from the
async example "
+ "of the Java EE Tutorial. It was sent on "
+ dateFormatter.format(timeStamp) + ".";
message.setText(messageBody);
message.setSentDate(timeStamp);
Transport.send(message);
status = "Sent";
logger.log(Level.INFO, "Mail sent to {0}", email);
} catch (MessagingException ex) {
logger.severe("Error in sending message.");
status = "Encountered an error";
logger.severe(ex.getCause().getMessage());
}
return new AsyncResult<String>(status);
}
}
Please let me know what I was missing or it is GlassFish 3.1 Open Source bug.
Thank you in advance.
--
[Message sent by forum member 'vietnamusa']
View Post: http://forums.java.net/node/786974