Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

java - 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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...