Develop a web application using Tomcat
Create project
- Choose a
Java Enterprise
project and select theWeb application
Framework, select your application server( If you don’t have one in idea, just click new and choose the root folder of your server to create one). - Click Finish and you are ready to write your servlet
Add JDBC support
using DataSource and JNDI( taking mysql as an example)
- add driver to
server-root-folder/lib/
- add
context.xml
tocontext-root/META-INF/
(create one if not exist) with the following content:
<Context>
<Resource name="jdbc/YourJNDIRegisteredName" auth="Container" type="javax.sql.DataSource" maxTotal="50" maxIdle="30" maxWaitMillis="10000" logAbandoned="true" username="root" password="" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/YourMysqlDBName?autoReconnect=true"/>
</Context>
remember to change the user, password and db name in above file
This will register the mysql database to this server using JNDI, which you can access it later by right properties and a simple
context.lookup(Name)
Develop EJB (using wildfly)
- create a EJB project
- add bean/interface
- @remote
- @stateless/stateful - change the artifacts of default EJB project from
EJB Application: Exploded
toJAR
orEJB Application: Archive
(I don’t know whether it is a bug of wildfly and/or intellij on my platform, but change it indeed make it work. ).
I didn’t change the artifact type at the beginning which cause a serial of errors as the end of post shows. It fails with the error message – Services with missing/unavailable dependencies
is somewhat misleading for the beginner like me.
In order to figure out what wrong with it, I have to run same project in Eclipse which uses xxx.jar
to deploy project in <server-home>/standalone/deployments/
, even though this behavior is discouraged .
[org.jboss.as.server.deployment.scanner] (MSC service thread 1-6) WFLYDS0013: Started FileSystemDeploymentService for directory <server-home>/standalone/deployments
Users, particularly those running production systems, are encouraged to use the JBoss AS management APIs to upload and deploy deployment content instead of relying on the deployment scanner subsystem that periodically scans this directory. ———— “server-home”/standalone/deployments/README.txt
And through comparison between Eclipse and Intellij, I came to realize that it is the way that class packaged differs.
EJB Application: Exploded
:
This artifact is an exploded directory where files and folders are presented in the file system as separate itemsJAR
: just a zip file contain the compilation output
So I changed the artifacts of idea and it works like magic.
Invoke EJB bean in the client
- add
jboss-client.jar
: Setup the client classpath to include the jboss-client jar that’s required for remote invocation of the EJBs( it will analyse the jboss-ejb-client.properties to connect to server). The location of the jar isWILDFLY_HOME/bin/client/jboss-client.jar.
. - You’ll also need to have your application’s interface for bean and other jars that are required by your application, in the client classpath
- jboss-ejb-client.properties: The client will have to place this file in the classpath of the application. A sample is like following:
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
This will setup a EJB receiver which knows its target address is localhost:8080 which will be capable enough to communicate to the server via the JBoss specific EJB remote client protocol.
Notice:
- I am using a web project as ejb client to invoke the ejb server, so I have to put the jboss-client.jar file under the
WEB-INF/lib
and add it to artifact to make it work.
Add data source and using jpa
- add
module.xml
and connector.jar inserver_home/modules/system/layers/base/com/mysql/main/
- add
<datasource>
,<driver>
(missing this will also cause error ofmissing/unavailable dependencies
) instandalone.xml
- add your table mapped entity class
- start your mysql server before start wildfly server otherwise communication failure(
connection refused error
) will show
Unsolved error:
invisible char behind the tag
: A strangestandalone.xml
parse error. It soon disappear after re-write some xml setting.
More Errors you may encounter:
wrong url cause wrong drive class error
<datasource jta="true" jndi-name="java:/MySqlStudentDS" pool-
name="MySqlDS" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql:/localhost:3306/student</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password></password>
</security>
</datasource>
18:27:58,936 WARN [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (ServerService Thread Pool -- 58) IJ000604: Throwable while attempting to get a new connection: null: javax.resource.ResourceException: IJ031084: Unable to create connection
at ...
Caused by: javax.resource.ResourceException: IJ031083: Wrong driver class [com.mysql.jdbc.Driver] for this connection URL [jdbc:mysql:/localhost:3306/student]
at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:314)
... 32 more
So this make me remember the url should be jdbc:mysql://localhost:3306/sutdent
which has two backslashes before the ip address!
No persistence.xml when using @PersistenceContext:
WFLYJPA0033: Can't find a persistence unit named null in deployment \"TryEJB.jar\""},
which means that I didn’t add persistence.xml to specify a persistence unit.
java.lang.ClassNotFoundException: org.hibernate.collection.internal.PersistentSet
The client need hibernate core jar to receive the collections come from jboss server or you will meet the error like this. I add the hibernate-core-4.3.9.Final.jar
to artifact and fix it.
java.lang.IllegalStateException: No EJB receiver available for handling
[appName:xxx,modulename:xxx,distinctname:xxx] combination for invocation context org.jboss.ejb.client.EJBClientInvoc
ationContext@xxxx
Possible causes:
- wrong JNDI path string to lookup: Check your string with the output of EJB container when deploying. The best way is to copy the output from server and add some slash and distinct name then paste to your client code.
- missing
jndiProperties.put("jboss.naming.client.ejb.context", true);
as here state.
-
java.lang.IllegalArgumentException: node to traverse cannot be null!
Possible causes:
entityManager.createQuery(someNamedQuery);
// using the wrong method. change it to
entityManager.createNamedQuery(someNamedQuery);
What I have learnt from it:
- Of course, the knowledge of Java EE and some miscellaneous knowledge.
- The way to analyze complex error message: find the root cause
- The way to find a solution: what to do, when the error message is not clear enough for you to fix it or even google it can’t find the same problem. I have to use somewhat stupid but useful way: comparison.
- I use Eclipse and Intellij to run same code with same server to find the problem of Artifact.
- I use others and my configuration file to run same code to find my configuration lack a backslash
Ref:
The long strange error messagem which fixed by change artifact.
ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC000001: Failed to start service jboss.deployment.unit."learnEJB_ejb_exploded.rar".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.unit."learnEJB_ejb_exploded.rar".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment "learnEJB_ejb_exploded.rar"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:163)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration
at org.jboss.as.connector.deployers.ra.processors.ParsedRaDeploymentProcessor.process(ParsedRaDeploymentProcessor.java:231)
at org.jboss.as.connector.deployers.ra.processors.ParsedRaDeploymentProcessor.deploy(ParsedRaDeploymentProcessor.java:129)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:156)
... 5 more
Caused by: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration
at org.jboss.jca.common.metadata.spec.ResourceAdapterImpl.validate(ResourceAdapterImpl.java:228)
at org.jboss.jca.common.metadata.spec.ConnectorImpl.validate(ConnectorImpl.java:350)
at org.jboss.as.connector.deployers.ra.processors.ParsedRaDeploymentProcessor.process(ParsedRaDeploymentProcessor.java:205)
... 7 more
10:24:27,634 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 2) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "learnEJB_ejb_exploded.rar")]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"learnEJB_ejb_exploded.rar\"
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration
Caused by: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration"},
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.InstanceName is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]",
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.InAppClientContainer is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]",
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.Validator is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]",
"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".batch.environment is missing [jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".beanmanager]",
"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".beanmanager]",
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.ValidatorFactory is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]"
]
}
10:24:27,638 ERROR [org.jboss.as.server] (management-handler-thread - 2) WFLYSRV0021: Deploy of deployment "learnEJB_ejb_exploded.rar" was rolled back with the following failure message:
{
"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"learnEJB_ejb_exploded.rar\"
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration
Caused by: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration"},
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.InstanceName is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]",
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.InAppClientContainer is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]",
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.Validator is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]",
"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".batch.environment is missing [jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".beanmanager]",
"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".beanmanager]",
"jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.ValidatorFactory is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]"
]
}
...
Artifact learnEJB exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"learnEJB_ejb_exploded.rar\"
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration
Caused by: org.jboss.jca.common.api.validator.ValidateException: IJ010075: The resource adapter metadata must contain either an outbound or inbound configuration"},"WFLYCTL0180: Services with missing/unavailable dependencies" => ["jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".beanmanager]","jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.Validator is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]","jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".batch.environment is missing [jboss.deployment.unit.\"learnEJB_ejb_exploded.rar\".beanmanager]","jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.InstanceName is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]","jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.ValidatorFactory is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]","jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB.InAppClientContainer is missing [jboss.naming.context.java.comp.learnEJB_ejb_exploded.learnEJB_ejb_exploded.HelloEJB]"]}
Written with StackEdit.
评论
发表评论