2014-05-14 40 views
0


我正在使用.NET 3.5將消息發送到活動mq,並且我有一個從隊列中處理這些消息的java偵聽器。如何以long(datatype)的形式發送消息給activemq負載?

部首包含

userId - long 
type - string 
isAdd-bool 

監聽期待有效載荷是數據類型的長。所以我應該在有效載荷中發送數據類型。

這裏是我用來發送消息到活動mq的代碼。

string payLoad = "123";  
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616"); 
        using (IConnection connection = factory.CreateConnection()) 
        { 
         using (ISession session = connection.CreateSession()) 
         { 
          IDestination destination = Apache.NMS.Util.SessionUtil.GetDestination(session 
           , "Message.AddUser"); 
          using (IMessageProducer producer = 

session.CreateProducer(destination)) 
          { 
          // Start the connection so that messages will be processed. 
          connection.Start(); 

          //here i need to pass payload as datatype "long" 
          ITextMessage request = session.CreateTextMessage(payLoad); 
          request.Properties["userid"] = 123; 
          request.Properties["type"] = "USER"; 
          request.Properties["isAdd"] = true; 
          producer.Send(request); 

         } 

        } 
       } 

當前按照代碼,我發送字符串作爲有效負載。

ITextMessage request = session.CreateTextMessage(payLoad); 

我該如何更改此代碼以發送有效載荷作爲數據類型很長?我試圖發送有效載荷作爲對象。但是,當聽衆選擇此消息時,我收到錯誤消息。錯誤:無效的流標頭已損壞。

按照註釋,我用IStreamMessage

  int userId = 123; 
      IStreamMessage message = session.CreateStreamMessage() 
      message.WriteInt64((long)idToUpdate); 
      message.Properties.SetLong("userId", (long)userId); 
      message.Properties.SetString("type", "USER"); 
      message.Properties.SetBool("isDelete", true); 

然後我看着Tomcat的日誌,這是我能看到,標題中缺少這一點。我只有有效負載

[Payload=ActiveMQStreamMessage {commandId = 5, responseRequired = true, messageId = ID:XXXXX-60790- 
stamp=1400070001928, jms_redelivered=false, userId=123, type=USER, jms_messageId=ID:XXXXX-60790-635356865723233336-0:0:1:2:1}] 

由於這不起作用,我嘗試使用ITextMessage。

int userId = 123; 
ITextMessage request = session.CreateTextMessage(); 
request.Text = idToUpdate.ToString(); 
request.Properties["companyId"] = (long)userId; 
request.Properties["type"] = "USER"; 
request.Properties["isDelete"] = true; 

這是我能看到的日誌。

[Payload=46][Headers={timestamp=1400072608501, id=dde638d2-4036-4e81-a3c8-97937ac11087, isDelete=true, jms_timestamp=1400072608280, jms_redelivered=false, userId=1, type=USER, jms_messageId=ID:xxxx-53593-635356892076410652-0:0:1:2:1}] 

我使用Sprint整合和聽衆期待列表作爲輸入參數

public void updateUser(Message<List<Long>> message) { 
    Long userId = (Long) message.getHeaders().get("userId");  
    for (Long userId : message.getPayLoad()) { // exception is thrown here "Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long" 
     //doing somethign here with userid 
    } 
} 

如何發送有效載荷,其長度數據類型的?

+0

最後一點聽起來像StreamMessage中的一個錯誤,你使用的是最新版本的庫嗎?您是否嘗試過使用BytesMessage。 –

+0

感謝Tim的迴應,我會嘗試使用BytesMessage,並保持您的發佈相同。 – nimi

回答

0

你有你如何實現一對夫婦的選擇。

  • 您可以使用BytesMessage和使用,對多頭操作的讀/寫方法,但沒有類型檢查,有那麼確保你的客戶都做正確的事。
  • 您可以使用長時間讀/寫的StreamMessage,並檢查類型並根據需要進行一些類型轉換(如果需要)。
  • 您可以使用MapMessage並將long存儲在鍵/值映射中。
  • 您可以將TextMessage的文本解析爲使用標準Java代碼的很長時間。

你不能真正使用ObjectMessage。NET和Java,你也不想在這種情況下,因爲它是一個簡單的基本類型。

+0

我糾正了。沒有注意到客戶端是Java –

+0

IStreamMessage消息=新的ActiveMQStreamMessage(); ()(long)idToUpdate); // 1 message.Properties.SetLong(「userId」,(long)userid); // 123 message.Properties.SetString(「type」,headerType.ToString() ); // USER message.Properties.SetBool(「isDelete」,isDeleteAction); // true 當我查看我的有效負載時, [Payload = ActiveMQStreamMessage {commandId = 5,responseRequired = true,messageId = ID:XXXXX- 60790- stamp = 1400070001928,jms_redelivered = false,userId = 1,type = USER,jms_messageId = ID:XXXXX-60790-635356865723233336-0:0:1:2:1}] 我有hve標題 – nimi

+0

我沒有知道這應該是什麼意思。 –