JBoss configuration uses JMS Message Queuing

Source: Internet
Author: User
Tags message queue xmlns jboss jboss server
 Create a message queue on the JBoss server

We can create message queues in the following 4 ways: Management Console Management CLI deployment *-jms.xml file to deployments directory edit JBoss configuration file using Management Console creating Message Queuing

1. Start JBoss with messaging, that is, when you start JBoss, use the-C or--server-config= to a configuration file that points to JBoss Standalone-full.xml

./standalone.sh-c Standalone-full.xml

2. Log in to Management Console (http://localhost:9990/console), select Profile→messaging→destinations→default→view, then click Add button to create the message queue "Jms/queue/test"
creating message queues using the management CLI

1. Connect to the JBoss Management CLI (requires JBoss is boot state)

./jboss-cli.sh

2. Go to messaging Subsystem (need to connect to Jboss,connect directory default connection to localhost:9999)

Cd/subsystem=messaging/hornetq-server=default

3. Execute the Create Message Queue command
./jms-queue=testqueue:add (durable=false,entries=["Java:jboss/exported/jms/queue/test"])

Create a message queue using the deployment *-jms.xml file to the deployments directory

1. Create an XML file, arbitrarily named, for example, we create sample-jms.xml, the content is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <messaging-deployment
xmlns= "urn:jboss:messaging-deployment : 1.0 ">
    

Note the schema file for creating Message Queuing, etc. jboss_home/docs/schema/jboss-as-messaging-deployment_1_0.xsd As above Sample-jms.xml is created from this schema file.

2. Deploy Sample-jms.xml to JBoss

Use the previous series three 4 ways to deploy any one of the methods shown in the application to Jboss7/wildfly Sample-jms.xml to JBoss
Create a message queue by using the edit JBoss configuration file

1. Open Jboss_home/standalone/configuration/standalone-full.xml File

2. After </jms-connection-factories> in <subsystem xmlns= "urn:jboss:domain:messaging and

<jms-destinations>
        <jms-queue name= "Testqueue" >
            <entry name= "Queue/test"/>
            < Entry name= "Java:jboss/exported/jms/queue/test"/>
        </jms-queue>
</jms-destinations>

3. Save and close the open file Create a secure user on the JBoss server

Since JBoss 7 does not support anonymous connections, we need to create a application User, as in the following steps:

1. Open the command line terminal to create a user-initiated script based on your operating system execution.

linux:jboss_home/bin/add-user.sh

Windows:jboss_home\bin\add-user.bat

2. The following output will be available after startup

What type of user does wish to add?

A) Management user (mgmt-users.properties)
b) application User (Application-users.properties)
(a): at the

prompt, type:b

3. Create the user and password as follows
Enter the details of the new user to add.
Realm (Applicationrealm):

If We want to specify a realm, type it here. Otherwise, press ENTER to use the default Applicationrealm when

    prompted, enter the the the Username and Passord.

Username:admin
Password:!admin123
Re-enter Password:

4. Assigning the guest role to the created user
What roles does want this user to belong? (Please enter a comma separated list, or leave blank for none) [  ]: Guest

using the created message queue

This section includes sending messages to the message queue "Jms/queue/test" created above and receiving messages from "Jms/queue/test", and we use simple code emulation. Get Sample Code

The sample code is obtained according to the GITUHB installation section of the previous software installation and data download as follows:
We can import this code into Eclipse and add Jboss-client.jar (jboss_home/bin/client directory) to Classpath, which requires you to start JBOSS before running HELLOWORLSJMS:
run the sample code

Running HELLOWORLSJMS will have the following output:

Create Local JNDI Context successful
attempting to acquire connection factory "Jms/remoteconnectionfactory"
Found Connection Factory "Jms/remoteconnectionfactory" in JNDI
attempting to acquire destination "Jms/queue/test" C5/>found destination "Jms/queue/test" in JNDI
create Connection Factory successful
create producer successful< C8/>create consumer successful
sending 3 messages with Content:hello world, jms!
Received message 1 with content [Hello World, jms!]
Received message 2 with content [Hello World, jms!]
Received message 3 with content [Hello World, jms!]

As shown on our client connected to JBoss, the Message Queuing "Jms/queue/test" created producer and Consumer;producer continuously sent three messages, the message content is the string "Hello World, jms!" The consumer also received three consecutive messages.

The following is the code details for HELLOWORLSJMS:

Import java.util.Properties;
Import javax.jms.Connection;
Import Javax.jms.ConnectionFactory;
Import javax.jms.Destination;
Import Javax.jms.MessageConsumer;
Import Javax.jms.MessageProducer;
Import javax.jms.Session;
Import Javax.jms.TextMessage;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;

Import javax.naming.NamingException;  public class Helloworlsjms {private Context GetContext () throws namingexception{final Properties env
        = new Properties ();          env.put (Context.initial_context_factory, "
        Org.jboss.naming.remote.client.InitialContextFactory ");
                  env.put (Context.provider_url, "remote://localhost:4447");
                 Env.put (Context.security_principal, "admin");
        
            Env.put (context.security_credentials, "!admin123");
  return new InitialContext (env);
 } public void Test () throws Exception {   
    System.out.println ("JMS HelloWorld start");
    
    Context CTX = GetContext ();
    
    System.out.println ("Create Local JNDI Context successful");
        ConnectionFactory connectionfactory = null;
        Connection Connection = null;
        Session session = NULL;
        MessageProducer producer = null;
        Messageconsumer consumer = null;
        Destination Destination = null;
    
    TextMessage message = NULL;
      try {String connectionfactorystring = "Jms/remoteconnectionfactory";
      System.out.println ("Attempting to acquire connection factory \" "+ connectionfactorystring +" \ "");
      ConnectionFactory = (connectionfactory) ctx.lookup (connectionfactorystring);
      
      SYSTEM.OUT.PRINTLN ("Found connection factory \" "+ connectionfactorystring +" \ "in JNDI");
      String destinationstring = "Jms/queue/test";
            System.out.println ("attempting to acquire destination \" "+ destinationstring +" \ "); Destination = (destination) Ctx.lookup (destinationstring);
            
            System.out.println ("Found destination \" "+ destinationstring +" \ "in JNDI"); Create the JMS connection, session, producer, and Consumer connection = Connectionfactory.createconnection (
            "Kylin", "Redhat");
            System.out.println ("Create Connection Factory successful");
            Session = Connection.createsession (false, Session.auto_acknowledge);
            Producer = Session.createproducer (destination);
            System.out.println ("Create producer successful");
            Consumer = session.createconsumer (destination);
            SYSTEM.OUT.PRINTLN ("Create consumer successful");
            
            Connection.start ();
            int count = 3;

            String content = "Hello world, jms!";
            
            SYSTEM.OUT.PRINTLN ("sending" + Count + "messages with content:" + content); Send the specified number of messages for (int i = 0; I &lT Count
                i++) {message = Session.createtextmessage (content);
            Producer.send (message);
                }//Then receive the same number of Messaes this were sent for (int i = 0; i < count; i++) {
                Message = (textmessage) consumer.receive (5000);
            SYSTEM.OUT.PRINTLN ("Received message" + (i + 1) + "with content [" + message.gettext () + "]");
    }} catch (Exception e) {throw e; } finally {//closing the connection takes care of the session, producer, and consumer if (Conne
            ction = null) {connection.close ();
  }}} public static void Main (string[] args) throws Exception {new HELLOWORLSJMS (). Test (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.