1) The JMS queue is first configured in JBOSS8 when using JBOSS8, or wildfly, for MDB experiments.
I configured the JMS queue queue in a way that modifies the configuration file.
Enter the JBOSS8 installation directory under the Standalone\configuration folder:
Because you are using the Standalone-full.xml file, modify the file name to Standalone.xml
Search the file under the
<jms-destinations> <Jms-queuename= "Myqueue"> <entryname= "Jms/queue/myqueue"/> <entryname= "Java:jboss/exported/jms/queue/myqueue"/> </Jms-queue> </jms-destinations>
Then open the JBOSS8 console (you need to use the Add-user.bat feature to set a user name), click the Runtime interface Jndi View if you can find the content shown is configured successfully:
In this figure, Remoteconnectionfactory is the connection factory used in the client, and Myqueue is the configured JMS Queue.
2) Writing an MDB
The Java code is as follows:
ImportJavax.ejb.ActivationConfigProperty;ImportJavax.ejb.MessageDriven;ImportJavax.jms.Message;ImportJavax.jms.MessageListener;Importjavax.jms.TextMessage; @MessageDriven (Activationconfig={@ActivationConfigProperty (propertyname= "Destinationlookup", PropertyValue = "Queue/myqueue"), @ActivationConfigProperty (PropertyName= "DestinationType", PropertyValue = "Javax.jms.Queue"), @ActivationConfigProperty (PropertyName= "User", PropertyValue = "Jadmin"), @ActivationConfigProperty (PropertyName= "Password", PropertyValue = "Jadminjadmin") }) Public classMyqueuemdbbeanImplementsMessageListener {@Override Public voidonMessage (Message msg) {Try{Thread.Sleep (10000); TextMessage TM=(TextMessage) msg; System.out.println ("MDB received a message =" +Tm.gettext ()); } Catch(Exception e) {e.printstacktrace (); } }}
Because JBOSS8 is a non-anonymous access, configure the user name and password in the annotations, which can be configured using Add-user.bat, configure application user, and the permissions can be the guest
The configuration of the user name and password is set as Client access.
3) Client
The Java code looks like this:
Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;ImportJavax.jms.MessageProducer;ImportJavax.jms.Queue;Importjavax.jms.QueueSession;Importjavax.jms.Session;ImportJavax.jms.TextMessage;ImportJavax.naming.Context;ImportJavax.naming.InitialContext; Public classpoint2pclient { Public Static voidMain (string[] args) {Try { //Properties Jndiproperties = new properties (); // //jndiproperties.put (Context.initial_context_factory,//"Org.jboss.naming.remote.client.InitialContextFactory"); // //jndiproperties.put (Context.provider_url, "http-remoting://localhost:8080 "); //jndiproperties.put (Context.security_principal, "admin"); //jndiproperties.put (context.security_credentials, "adminadmn"); //jndiproperties.put ("Jboss.naming.client.ejb.context", true);Context context =NewInitialContext (); ConnectionFactory Factory=(connectionfactory) context. Lookup ("Jms/remoteconnectionfactory"); Queue Destination= (Queue) context.lookup ("Jms/queue/myqueue");//Destination Destination = (Destination) Context//. Lookup ("Jms/queue/myqueue");Connection Connection= Factory.createconnection ("Jadmin", "Jadminjadmin"); When you create a connection using factory, the application user that is configured in the second step is added to create a connection session session that connects the MDB= Connection.createsession (false, Queuesession.auto_acknowledge); TextMessage msg= Session.createtextmessage ("This is a text: 13311PPPPP"); MessageProducer Sender=session.createproducer (destination); Sender.send (msg); Session.close (); Connection.close (); System.out.println ("+++++success"); } Catch(Exception e) {e.printstacktrace (); } }}
The configuration file is as follows:
Java.naming.factory.initial=org.jboss.naming.remote.client.initialcontextfactoryjava.naming.provider.url= http-remoting://localhost:8080
In Jboss8, the number of ports consumed is greatly reduced, many functions are concentrated on port 8080, and of course can be modified (JBOSS7 4447)
In addition, you need to include two jar files from the client folder in the JBOSS8 installation directory in the clients ' Classpath
4) The results of the operation are as follows:
16:34:34,199 INFO [org.jboss.as.naming] (default task-4) Jbas011806:channel end notification received, closing Channel channel ID 0478d991 (inbound) of Remoting connection 79187fc1 to null16:34:44,144 INFO [stdout] (Thread-12 (Ho rnetq-client-global-threads-1153747233)) MDB received a message = This is a text: 13311PPPPP
Precautions * * *
Jboss8 and JBOSS5 differ significantly, can be compared to Jboss5+ejb3+mdb Queue
This article only analyzes the configuration of the JMS queue, topic is similar to the queue, you can try to modify
1. Client Exception 1:
javax.naming.namenotfoundexception:remoteconnectionfactory--service Jboss.naming.context. Java.jboss.exported.RemoteConnectionFactory at Org.jboss.as.naming.ServiceBasedNamingStore.lookup (servicebasednamingstore.java:104) at Org.jboss.as.naming.NamingContext.lookup (namingcontext.java:202) at Org.jboss.as.naming.NamingContext.lookup ( namingcontext.java:179) at Org.jboss.naming.remote.protocol.v1.protocol$1.handleservermessage (Protocol.java:127) At Org.jboss.naming.remote.protocol.v1.remotenamingserverv1$messagereciever$1.run (RemoteNamingServerV1.java:73) 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)
Indicates that the remoteconnectionfactory used in the client cannot match the factory in the JBoss server Jndi and cannot be found.
Standalone.xml file to find information about Remoteconnectionfactory:
<connection-factoryname= "Remoteconnectionfactory"> <Connectors> <Connector-refConnector-name= "Http-connector"/> </Connectors> <Entries> <entryname= "java:jboss/exported/jms/remoteconnectionfactory"/> </Entries> </connection-factory>
The bold portion of the two-segment code of the contrast surface reveals that the JMS field is missing from the exception information, so use Jms/remoteconnectionfactory when the client creates the connection
2. Client Exception 2
unable to validate User:null]
This exception is mainly when using the factory to create connection, do not use the application user corresponding to the MDB
Jboss8+ejb3+mdb Queue