ACTIVEMQ and Rest API practice __ACTIVEMQ

Source: Internet
Author: User
Tags http post

Summary

1. How to build an active rest service environment

2. REST API and related configuration parameter introduction

3. Introduction of Optimizeack mechanism in ACTIVEMQ

ActiveMQ implements the RESTful API, which allows developers to post and consume messages directly via HTTP Post/get, a protocol based on text (/file) transmission. Because in some particular environment, It is easier to manipulate messages via HTTP, such as using JavaScript or form events to get messages on HTML5 mobile terminals.

Typically, in an open Internet environment, in a Non-core message/class log system, we use ACTIVEMQ rest to push (save)/delete (consume) messages. We can disguise the rest service as a log storage and distribution system, and we can implement storage and distribution based on large data messages without the need for additional framework support. You can also use rest in the Web Brower, polling or keepalive to implement a real-time message push.

The most important feature of the rest protocol is more Internet-oriented applications, not enterprise-wide applications its interaction is convenient, the data readability is strong. Of course its limitations are also obvious: on the security/message ACK mechanism/transaction support/and the asynchronous operation of the message (relative to Openwire). Both consumers and producers need to describe the operation through URLs, which limits the client-side tuning under the rest protocol and is limited by the HTTP protocol (: keep-alive) and the servlet container's ability to support asynchrony.

Each ACTIVEMQ broker integrates the servlet environment (the default is the jetty environment), and when the ACTIVEMQ broker is started, The default mount boot. Org.apache.activemq.web.MessageServlet is an officially implemented rest proxy class that is used to broker rest requests. The implementation principle is also very simple, the servlet receives the request, the internal will use the ordinary JMS API + Openwire protocol and broker communication. In the rest agreement, acitvemq the URL format as well as the parameter list, and the publish message must be based on an HTTP POST, and the consumer message can be based on the Get/delete method.     Java Code >>>http://localhost:8161/api/message/test-queue?type=queue&clientid=test1&json=true hello,restful!

1. Environmental Preparedness

ACITVEMQ Broker will mount the launch of multiple Web systems, one of which is the "API", and you can see the configuration in {Activemq}/webapps/api. This API system is serviced for rest agreements. The {Activemq}/conf/activemq.xml file, which is the portal to all ACTIVEMQ configuration files, can use the API system by introducing a configuration into the jetty environment. Java code <import resource= "Jetty.xml"/>

Java code <transportconnector name= "Rest" uri= tcp://0.0.0.0:51616?maximumconnections=1000& Wireformat.maxframesize=1048576&keepalive =true&sotimeout =30000 "/>

In addition, because the rest layer is still based on Openwire (TCP channel) support, and rest characteristics need to be close to the HTTP "short"/"small data" characteristics, so we in the Transportconnector, add the URI sotimeout= 3000, and control the maximum data size of 1M.

From Jetty.xml, we can see configuration information for all Web systems: Java code <bean id= "Securityconstraint" class= " Org.eclipse.jetty.util.security.Constraint "> <property name=" name value= "BASIC"/> <property name = "Roles" value= "admin"/> <!--Use security authentication--> <property name= "Authenticate" value= "true"/> ;/bean> <bean id= "securityconstraintmapping" class= "org.eclipse.jetty.security.ConstraintMapping" > <PR Operty name= "constraint" ref= "Securityconstraint"/> <property name= "Pathspec" value= "/*"/> </bean> ;

After 5.8, all web systems in the jetty environment are authorized by default, which means that the rest API is required to provide "name" + "password" checksum, otherwise the rest request will be rejected. Of course, Developers can note that the following parameter rest no longer requires authentication. Java code <!--whether to use authorization authentication, "true"/"false"--> <!--<property name= "Authenticate" value= "true"/>

In addition, letting ACTIVEMQ broker provide rest services does not seem to be a good architectural design, which limits the developer's extensibility to rest services, and in many cases we need to migrate rest services to specialized Web applications . Then we don't need to have the jetty environment mount the API system. Java code <!--<bean class= "Org.eclipse.jetty.webapp.WebAppContext" > <property name= "contextpath" value = "/api"/> <property name= "resourcebase" value= "${activemq.home}/webapps/api"/> "<property name=" L Ogurlonstart "value=" true "/> </bean>-->

Next, we will build our own ACTIVEMQ rest service environment.

2. configuration file

If you are based on ACTIVEMQ jetty environment, you may need to check out the {Activemq}/webapps/api/web-inf/web.xml file if your rest server is built yourself, You may need to copy the core information from this web.xml file to your project. In any case, you need to configure and start "Org.apache.activemq.web.MessageServlet" in the servlet environment. where "Org.jolokia.http.AgentServlet" is only optional, for their own rest services, Jolokia need not be configured.

To build a servlet system, we must first determine the servlet version, as this will directly relate to the asynchronous nature of rest, and the more stable version of the current combination is: servlet 2.4 (2.5) + Tomcat 6.x,servlet 3.0.1 + Tomcat7.x,servlet 3.1 + tomcat8.x. In servlet2.x and servlet3.x, there is a slight difference in web.xml configuration, mainly because of the way they control the "asynchronous" operation, which is called asynchronous, when the message is consumed in the rest request, if there is no message in queue/topic at this time, The request is suspended until a message is available for consumption.

Tomcat configuration (Tomcat 6.x+)

Java code <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443" uriencoding= "Utf-8" usebodyencodingforuri= "false" maxthreads= "the Maxkeep" Aliverequests= "/>" Protocol Agreement for "http/1.1", if you specify "http/1.0", then may not be good use of "keep-alive", need some additional control, this will no longer be said. The ConnectionTimeout can be adjusted according to the actual situation, to control the connector release of the link before blocking time, by default KeepAliveTimeout and ConnectionTimeout values, used to describe the time the link remains active.

pom.xml (servlet-2.4) Java Code    <dependency>       <groupid>javax.servlet</ groupid>       <artifactId>servlet-api</artifactId>       <version>2.4</version>       <scope> provided</scope>   </dependency>   <dependency>        <groupId>org.mortbay.jetty</groupId>       <artifactid >jetty-util</artifactId>       <version>6.1.26</version>    </dependency>   <dependency>       <groupid >org.apache.activemq</groupId>       <artifactid>activemq-web</ artifactid>       <version>5.8.0</version>  

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.