Dışarıya aktar (0) Yazdır
Tümünü Genişlet
EN
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.
7 / 9 bunu faydalı olarak değerlendirdi - Bu konuyu değerlendir

How to Use JMS with AMQP 1.0 in Azure with Eclipse

Updated: April 4, 2014

Advanced Message Queuing Protocol (AMQP) 1.0 is an efficient and reliable wire-level messaging protocol that can be used to build robust cross-platform messaging applications. AMQP 1.0 support has been added to Azure Service Bus as a preview feature. The following steps show you how to use the Java Message Service (JMS) with AMQP 1.0 in a basic JSP application deployed to Azure using the Azure Plugin for Eclipse with Java (by Microsoft Open Technologies).

  1. Create a dynamic web project: Within Eclipse, at the menu click File, click New, and then click Dynamic Web Project. (More detailed steps for creating a dynamic web project are available at Creating a Hello World Application for Azure in Eclipse.)

  2. Add a JSP file named index.jsp to your dynamic web project. Use the following code for index.jsp.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>AMQP 1.0 message sender</title>
    </head>
    <form method="post" action="sendMessages.jsp">
    <br/>
    This sample sends a message to a queue.
    <br/><br/>
    Enter your message: <input type="text" name="message" />
    <input type="submit" name="submit" value="Send Message" />
    </form>
    </html>
    
  3. Add a JSP file named sendMessages.jsp to your dynamic web project. Use the following code for sendMessages.jsp.

    <%@ page import="myAMQPPackage.MyAMQPSender" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>AMQP 1.0 page 2</title>
    </head>
    <body>
    <%
        out.print("Sending message -------> " + request.getParameter("message") );
        out.print("<br/>");
    
        try 
        {
            // Send the message.
            MyAMQPSender.postMessages(request.getParameter("message"));
            out.println("Message sent successfully\n");
        }
        catch (Exception e) 
        {
            out.println("Error occurred while sending message to queue.\n");
            out.println(e);
        }
    %>
    </body>
    </html>
    
  4. Create a Java class, MyAMQPSender, for your dynamic web project. For purposes of this tutorial, name the package that it belongs to myAMQPPackage. Use the following code for MyAMQPSender.java.

    package myAMQPPackage;
    
    import javax.jms.*;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import java.util.Hashtable;
    
    public class MyAMQPSender
    {
        
        private static Connection connection;
        private static Session sendSession;
        private static MessageProducer sender;
        private static Context context;
        
        
        public static void postMessages(String textMessage) throws Exception
        {
            try
            {
                init();
                sendTextMessage(textMessage);
                close();
            }
            catch (Exception e)
            {
                System.out.println("Exception: " + e);
            }
        }
    
        private static void sendTextMessage(String textMessage) throws JMSException 
        {
            TextMessage message = sendSession.createTextMessage();
            message.setText(textMessage);
            sender.send(message);
            System.out.println("Sent message with JMSMessageID = " + message.getJMSMessageID());
        }
        
        private static void init() 
        {
            try
            {
                // Configure JNDI environment
                Hashtable<String, String> env = new Hashtable<String, String>();
                env.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory");
                env.put(Context.PROVIDER_URL, "servicebus.properties");
                context = new InitialContext(env);
        
                // Lookup ConnectionFactory and Queue
                ConnectionFactory cf = (ConnectionFactory) context.lookup
                        ("SBCONNECTIONFACTORY");
                Destination queue = (Destination) context.lookup("QUEUE");
        
                // Create Connection
                connection = cf.createConnection();
        
                // Create sender-side Session and MessageProducer
                sendSession = connection.createSession(false,
                        Session.AUTO_ACKNOWLEDGE);
                sender = sendSession.createProducer(queue);
            }
            catch (Exception e)
            {
                System.out.println("Exception: " + e);
            }
        }
    
        private static void close() throws JMSException 
        {
            sender.close();
            sendSession.close();
            connection.close();
        }
    }
    
  5. Add the Package for Apache Qpid Client Libraries for JMS (by MS Open Tech) library to your dynamic web project’s build path and deployment assembly:

    1. Within Eclipse’s Project Explorer, right-click your dynamic web project and click Properties.

    2. In the left-hand pane of the Properties dialog, click Java Build Path.

    3. In the Java Build Path dialog, click the Libraries tab, click Add Library, select Package for Apache Qpid Client Libraries for JMS (by MS Open Tech), and then click Next.

    4. In the Add Library dialog, ensure Include in the project deployment assembly is checked.

    5. Click Finish to close the Add Library dialog.

    6. Click OK to close the Properties dialog.

  6. Create an Azure project for your application: In Eclipse’s Project Explorer, right-click your dynamic web project, click Azure, and then click Package for Azure. Provide values for the fields in the Azure Deployment Project dialog and click Finish. (For detailed instructions on this dialog, see Creating a Hello World Application for Azure in Eclipse.)

  7. Create a file named servicebus.properties. You can pick any local folder, the file will be added as a component in the next step.

    Use the following contents for servicebus.properties.

    # Register a ConnectionFactory in JNDI using the form:
    # connectionfactory.[jndiname] = [ConnectionURL]
    connectionfactory.SBCONNECTIONFACTORY = \
      amqps://owner:your_url_encoded_key@your_namespace.servicebus.windows.net
    
    # Register some queues in JNDI using the form:
    # queue.[jndiName] = [physicalName]
    queue.QUEUE = your_queue
    

    Use your Service Bus namespace name in place of your_namespace, and use your Service Bus access key in place of your_url_encoded_key. Note that within servicebus.properties, the Service Bus key needs to be URL-encoded. One technique for creating a URL encoding is via the web site http://www.w3schools.com/tags/ref_urlencode.asp.

    Use the name of the queue that you created in place of your_queue. Also, if your issuer is not owner, modify it in the connectionfactory.SBCONNECTIONFACTORY string too.

    For more information about servicebus.properties, see http://www.windowsazure.com/en-us/develop/java/how-to-guides/service-bus-amqp/.

  8. Add servicebus.properties as a component to your worker role:

    1. In Eclipse’s Project Explorer, expand your Azure project, right-click WorkerRole1, click Azure, and then click Components.

    2. In the Components dialog, click Add.

    3. In the Add Component dialog under Import, for From path, click File, and select your servicebus.properties file.

    4. For the Import method, use copy.

    5. For As name, use servicebus.properties, which will be the default when you click the text box.

    6. In the Add Component dialog under Deploy, for Method, use copy.

    7. For To directory, use %CATALINA_HOME%\bin.

    8. Click OK to close the Add Component dialog, and then click OK to close the Components dialog.

  9. To run in the compute emulator, in the Azure toolbar, click the Run in Azure Emulator icon to run your application in the compute emulator. After the application is running in the compute emulator, launch the localhost URL for your application. Enter a message and then click Send Message to see the result.

  10. To run in Azure, within the Azure toolbar, click the Publish to Azure Cloud icon and follow the prompts to complete your deployment. (More information about deploying to Azure can be found in Creating a Hello World Application for Azure in Eclipse.) After the application is running as an Azure cloud service, launch the URL for your application. Enter a message and then click Send Message to see the result.

Bunu faydalı buldunuz mu?
(1500 karakter kaldı)
Geri bildiriminiz için teşekkür ederiz
Show:
© 2014 Microsoft. Tüm hakları saklıdır.