泽西休息Web服务与Activemq中间件集成 [英] jersey rest web Service with Activemq middleware integration

查看:161
本文介绍了泽西休息Web服务与Activemq中间件集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JAX-RS和jersey开发的Restful服务API。我在TOMCAT 7中部署了相同的内容。现在我想实现Activemq,以便将所有请求保留在队列中并处理请求资源。如何做到这一点并与tomcat7集成。如何将ActiveMq与Tomcat7或我的休息服务webapp集成。如何调用该服务。

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.

重要: -
在Rest Api中,我使用FilterChaining概念来解决安全问题,在验证主叫方之后,我只是将请求转发给资源。为此我在web.xml中添加了。

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.

谢谢

这是我的班级: -

    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);
                }
            } 
            }
        }

这是我的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());
        }

    }

}

现在,如果有任何请求进入限制过滤器类,我会在一些身份验证mecanisam之后直接转发到资源。这就是为什么我使用过滤器概念来捕获所有请求。

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.

第二课是我跑步时的示例课;消息传递正在征服和出列;我可以在控制台中看到ActiveMq。

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

在第一堂课我只是写chain.doFilter(request,response)来将所有http请求转发到相应的资源。现在该怎么做放置HTTP请求的位置。我需要异步处理每个请求。 REST是同步的。

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.

谢谢/问候
Kumar Shorav

Thanks/Regards Kumar Shorav

推荐答案

您因为缺少凭据而在访问其他URL时看到ActiveMQ管理控制台的原因,请使用基本身份验证传递凭据。我写了一个groovy客户端来调用rest URL。你可以使用类似的东西....

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();

这篇关于泽西休息Web服务与Activemq中间件集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆