jersey rest web 服务与 Activemq 中间件集成 [英] jersey rest web Service with Activemq middleware integration

查看:33
本文介绍了jersey rest 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());
        }

    }

}

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

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.

谢谢/问候库马尔·肖拉夫

Thanks/Regards Kumar Shorav

推荐答案

由于缺少凭据,您看到 ActiveMQ 管理控制台同时点击 rest URL 的原因,请使用基本身份验证传递凭据.我写了一个 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();

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

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