2013-10-31 35 views
1

我有一個Restful服務API與JAX-RS和澤西開發。我在TOMCAT 7中部署了相同的部分。現在我想實現Activemq,以便將所有請求保留在隊列中並處理請求資源。如何做到這一點,並與tomcat7集成。如何將ActiveMq與Tomcat7或我的休息服務webapp集成。如何致電該服務。澤西休息網服務與Activemq中間件集成

重要提示: - 在Api中,我使用FilterChaining概念來實現安全性,並且在對主叫方進行驗證後,我只是簡單地將請求轉發給資源。爲此我已經在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後直接轉發到資源。這就是爲什麼我使用過濾器概念來捕獲所有請求。

第二課是當我跑步時的示例類;消息傳遞正在進行和退出;我可以在控制檯中看到ActiveMq。

在第一節中,我簡單地寫了「chain.doFilter(request,response)」將所有http請求轉發給相應的資源。現在該怎麼辦。何處放置HTTP請求。我需要異步處理每個請求。 REST是同步的。

請建議一些方法。並解釋你的答案。

謝謝/問候 庫馬爾Shorav

+0

downvoter請評論。這個問題出了什麼問題? – Kumar

+0

您對HttpServletRequest中的自定義對象感興趣嗎?或者您需要它,因爲您想稍後將信息傳遞迴客戶端? – Phani

+0

@Phani是的,在Httpservlet請求內。 – Kumar

回答

0

你已經看到的Apache ActiveMQ的REST的文檔:http://activemq.apache.org/rest.html

而且你說的是嵌入的ActiveMQ作爲內部Tomcat的經紀人,或者你在運行ActiveMQ代理單獨的盒子/ jvm?

由於獨立的ActiveMQ具有開箱即用的REST API,如上面鏈接中所述。

+0

我已經閱讀了這個文檔。但是,我無法從這一點開始。你有沒有一個小例子...請讓我知道。感謝您的回答。 – Kumar

+0

我不知道,但我需要將所有傳入的Http請求放入隊列,然後轉發到該服務。 – Kumar

0

請參考:http://java.dzone.com/articles/jms-activemq

其解決您的問題,很快讓發送消息一個功能,並納入在你不會處理在你。

+0

感謝您的時間。請參閱更新。我也閱讀過這篇文章。再次感謝。我正在努力解決這個問題。請參見。 – Kumar

+0

你有什麼問題?在配置或消息不是隊列中的PERSIST – jayesh

+0

我的問題是,當我打我的休息url它給xml或json字符串。但是現在在實施中間件之後;如何在servlet內部轉發請求並獲取相同的json/xml字符串。此外,當我點擊該請求時,它顯示了activemq「隊列」選項卡的管理控制檯。我是JMS和activemq中的新成員。謝謝 – Kumar

0

您在看到ActiveMQ管理控制檯時由於缺少憑據而點擊其餘的URL的原因,請使用基本身份驗證來傳遞憑據。我寫了一個groovy客戶端來調用其餘的URL。您可以使用類似的東西....

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