Wildfly Practice 5---JMS service access in distributed services

Source: Internet
Author: User

Practical conditions and objectives:

1. Configuration of master-slave services in distributed services

2. The main code snippet from the service shows

3. Use Wildfly10 this time, because its default JMS service is ACTIVEMQ.

Steps:

  • The primary service configuration uses the Standalone-full.xml boot, which comes with the ACTIVEMQ module.
  • Use AddUser to add application user name to guest, password to guest, role to guest, add application user Ejbuser, password 123.
  • Locate the ACTIVEMQ module in the configuration file and modify the configuration as follows:
            <Subsystemxmlns= "urn:jboss:domain:messaging-activemq:1.0">            <Servername= "Default">                <security-settingname="#">                    <rolename= "Guest"Delete-non-durable-queue= "true"Create-non-durable-queue= "true"Consume= "true"Send= "true"/>                </security-setting>                <address-settingname="#"Message-counter-history-day-limit= "Ten"page-size-bytes= "2097152"max-size-bytes= "10485760"expiry-address= "Jms.queue.ExpiryQueue"dead-letter-address= "Jms.queue.DLQ"/>                <Http-connectorname= "Http-connector"Endpoint= "Http-acceptor"socket-binding= "http"/>                <Http-connectorname= "Http-connector-throughput"Endpoint= "Http-acceptor-throughput"socket-binding= "http">                    <paramname= "Batch-delay"value= " the"/>                </Http-connector>                <In-vm-connectorname= "IN-VM"Server-id= "0"/>                <Http-acceptorname= "Http-acceptor"Http-listener= "Default"/>                <Http-acceptorname= "Http-acceptor-throughput"Http-listener= "Default">                    <paramname= "Batch-delay"value= " the"/>                    <paramname= "Direct-deliver"value= "false"/>                </Http-acceptor>                <In-vm-acceptorname= "IN-VM"Server-id= "0"/>                <Jms-queuename= "Expiryqueue"Entries= "Java:/jms/queue/expiryqueue"/>                <Jms-queuename= "DLQ"Entries= "JAVA:/JMS/QUEUE/DLQ"/>                <Jms-queuename= "Ticketqueue"Entries= "Java:jboss/exported/jms/queue/ticketqueue"/>                <connection-factoryname= "Invmconnectionfactory"Entries= "Java:/connectionfactory"Connectors= "IN-VM"/>                <connection-factoryname= "Remoteconnectionfactory"Entries= "Java:jboss/exported/jms/remoteconnectionfactory"Connectors= "Http-connector"/>                <pooled-connection-factoryname= "Activemq-ra"Transaction= "XA"Entries= "Java:/jmsxa java:jboss/defaultjmsconnectionfactory"Connectors= "IN-VM"/>            </Server>        </Subsystem>
    View Code

    Note: The Ticketqueue we create here must use Jndi in java:jboss/exported/format, otherwise the external machine cannot access

  • Write the Ticketqueue MDB and deploy it to the master server with the following main code:
    @MessageDriven (name = "Ticketbooked", Activationconfig ={@ActivationConfigProperty (propertyname= "DestinationType", PropertyValue = "Javax.jms.Queue"), @ActivationConfigProperty (PropertyName= "Destinationlookup", PropertyValue = "Java:jboss/jms/queue/ticketqueue") }) Public classTicketbookedmsgImplementsMessageListener {Private Final StaticLogger Logger = Logger.getlogger (ticketbookedmsg.class); /*** Default constructor. */     Publicticketbookedmsg () {//TODO auto-generated Constructor stub    }    /**     * @seemessagelistener#onmessage (Message)*/     Public voidonMessage (Message message) {//TODO auto-generated Method Stub        Try {            Finalstring text = Message.getbody (string.class); Logger.info ("Received Message" +text); } Catch(JMSException ex) {Logger.error ("Onmsg Error" +ex.tostring ()); }    }
    View Code

  • From the server you can start with Standalone.xml, which is the default boot configuration, which is modified here in this configuration file:
        <Subsystemxmlns= "urn:jboss:domain:naming:2.0">            <Bindings>                <External-contextname= "Java:global/jmsserver"Module= "Org.jboss.as.naming"class= "Javax.naming.InitialContext"Cache= "true">                    <Environment>                        < Propertyname= "Java.naming.factory.initial"value= "Org.jboss.naming.remote.client.InitialContextFactory"/>                        < Propertyname= "Java.naming.provider.url"value= "http-remoting://192.168.50.123:8080"/>                        < Propertyname= "Java.naming.security.principal"value= "Ejbuser"/>                        < Propertyname= "Java.naming.security.credentials"value= "123"/>                    </Environment>                </External-context>            </Bindings>            <remote-naming/>        </Subsystem>

    Note: This configuration adds an external contextual context that accesses the context of the remote host Jndi.

  • Write the service code from the host, fragment as follows:
    Context jmsctx = (initialcontext) context.lookup ("Java:global/jmsserver");    Destination    = (Queue) jmsctx. Lookup ("Jms/queue/ticketqueue");    Activemqconnectionfactory    CF= (activemqconnectionfactory) jmsctx. Lookup ("Jms/remoteconnectionfactory" );            Cf.setcalltimeout (+);    Jmscontext    jmsctx=cf.createcontext ("Guest", "Guest", "UI started");

  • The service that writes this code is deployed to the slave server and is called.
  • The following is displayed on the primary server:
    09:37:50,032 INFO  [COM.SANJIAN.HANDMSG.TICKETBOOKEDMSG] (Thread-96 (activemq-client-global-threads -30092228)) Received message UI started

  • In this context, the main framework of JMS communication for distributed services in WILDFLY10 has been completed in practice.

Wildfly Practice 5---JMS service access in a distributed service

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.