简体   繁体   中英

jersey rest web Service with Activemq middleware integration

I have a Restful service API developed with JAX-RS and jersey. I have deployed the same in TOMCAT 7. Now I would like to implement Activemq so that I would keep all request in a queue and process the request resource. How to do this and integrate with tomcat7. How to integrate ActiveMq with Tomcat7 or my rest service webapp. How to call the service.

Important :- Inside the Rest Api, I am using FilterChaining concept for security concern and after verification of the calling party, I am simply forwarding the request to the resource. For this I have added in web.xml.

Thanks

Here is my class :-

    public class LimitFilter implements Filter {

        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {

//some authentication
                if (true) {
                    // let the request through and process as usual
                    chain.doFilter(request, response);

                } else {
                    // handle limit case, e.g. return status code 429 (Too Many
                    // Requests)
                    // see http://tools.ietf.org/html/rfc6585#page-3
                    ((HttpServletResponse) response).sendError(429);
                }
            } 
            }
        }

Here is my sample class for activemq:-

public class Qservlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
            String body = "";
        try {
            // Create a ConnectionFactory

            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", ActiveMQConnection.DEFAULT_BROKER_URL);

            // Create a Connection

            Connection connection = connectionFactory.createConnection();


            Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

            Destination destination = session.createQueue("testQ"); 
            TextMessage message = session.createTextMessage();
            message.setText( "My text message was send and received");//
            message.setJMSRedelivered(true);
            message.setJMSCorrelationID(request.getSession().getId());

            connection.start();

            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            producer.send(message);

            message = null;
            MessageConsumer consumer = session.createConsumer(destination);
            message = (TextMessage) consumer.receive(1000);
            if (message != null) {
                body = message.getText();
            }


            producer.close();
            consumer.close();
            session.close();
            connection.close();

        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

}

Now if any request is coming in Limit Filter class, I am forwarding directly to the resources after some authentication mecanisam. Thats why I am using filter concept for catching all request.

Second class is example class when i am running ; messaging is enquing and dequeueing; I can see in console for ActiveMq.

In the first class I am simply writing "chain.doFilter(request, response)" to forward all http request to the respective resource. Now How to do here. Where to put the HTTP request. I need to handle each request asynchronously. REST is synchronous.

Please suggest some way. and explain your answer.

Thanks/Regards Kumar Shorav

Have you seen the REST documentation at Apache ActiveMQ: http://activemq.apache.org/rest.html

Also are you talking about embedding ActiveMQ as a broker inside Tomcat, or do you run ActiveMQ broker in a seperate box/jvm?

As the standalone ActiveMQ has REST API out of the box, as stated in that link above.

please refer : http://java.dzone.com/articles/jms-activemq

its solve your problem just make one function for send message and incorporate that in you process where you wont.

The reason you are seeing ActiveMQ admin console while hitting rest URL due to missing credentials, Please pass credentials using basic authentication. I wrote a groovy client to call rest URL. You can use something similar....

import groovyx.net.http.HTTPBuilder;
import groovyx.net.http.Method;
import groovyx.net.http.ContentType;
import groovyx.net.http.RESTClient;
import groovyx.net.http.HttpResponseDecorator;
import groovy.json.JsonSlurper;

def headers= ["Authorization": 'Basic'   +'admin:admin'.bytes.encodeBase64().toString()];
println headers;
def restClient = new RESTClient('http://servername:8161');
def restClientResponse = restClient.get(path: '/api/message/queueName?type=queue',headers:headers,requestContentType: ContentType.JSON)
println restClientResponse.status;
println restClientResponse.headers['Content-Length'];
println restClientResponse.getData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM