我是jms的新手, 我做了一个rabbitmq jms生产者项目,我在其中生成消息。在这个项目中使用了jmstemplate和rabbitmq队列(带有jms插件的rabbitmq)。
在队列中产生消息后,我想用简单的没有jms的rabbitmq使用者消费该消息。
但是当我在消费者类中将该消息作为字符串获取时。
以下格式的消息:
wx.com.rabbitmq.jms.client.message.RMQTextMessage $ e529erb2a65-a5dc-4bd7-bfe0-4c09237696261frmq.jms.message.destinationsr%com.rabbitmq.jms.admin.RMQDestinationH&安培)ZamqpZisQueueZisTemporaryLamqpExchangeNametLjava /郎/字符串;大号 amqpQueueNameq〜LamqpRoutingKeyq〜LdestinationNameq〜xptjms.durable.queuesttestq〜Q〜wrmq.jms.message.delivery.modermq.jms.message.timestamp [Yrmq.jms.message.priorityrmq.jms.message.expirationrmq.jms.message.id” ID:e529512742a65-a9dc-43467-fge4-4c092d42761fw {名称= abhijeet1}
最后我收到了“{name = abhijeet1}”的实际消息。
我如何才能获得实际信息?
是否可以使用rabbit jms producer和Consumer生成消息,使用简单的兔子mq Consumer消息?
如果可能的话?
我的制片人是:
package com.rabbitmq.jms.producer;
import java.util.HashMap;
import java.util.Map;
import javax.jms.ConnectionFactory;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.rabbitmq.jms.admin.RMQConnectionFactory;
@SpringBootApplication
@EnableScheduling
public class RabbitmqJmsProducerApplication {
@Autowired
JmsTemplate jmsTemplate;
int i;
@Bean
ConnectionFactory connectionFactory() {
RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
connectionFactory.setUsername("username");
connectionFactory.setPassword("password");
connectionFactory.setVirtualHost("**********");
connectionFactory.setHost("hostname");
return connectionFactory;
}
@Scheduled(fixedRate = 5000L) // every 5 seconds
public void publishQuote() {
i++;
MessageCreator messageCreator = (Session session) -> {
System.out.println("message sent " + "abhijeet " + i);
Map<String, String> map = new HashMap<>();
map.put("name", "abhijeet " + i);
return session.createTextMessage(map.toString());
};
jmsTemplate.send("test", messageCreator);
}
public static void main(String[] args) {
SpringApplication.run(RabbitmqJmsProducerApplication.class, args);
}
}
和消费者代码是:
package com.rabbitmq.consumer;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
public class MailConsumer implements Runnable {
private static boolean processMessges(Delivery delivery) throws Exception {
String msg = new String(delivery.getBody(), "UTF-8");
System.out.println("message is "+msg);
return true;
}
@Override
public void run() {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("username");
factory.setPassword("password");
factory.setVirtualHost("**********");
factory.setHost("hostname");
Connection connection;
connection = factory.newConnection();
Channel channel = connection.createChannel();
boolean removeAllUpTo = true;
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("test", consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
if (delivery == null)
break;
if (processMessges(delivery)) {
try {
long deliveryTag = delivery.getEnvelope().getDeliveryTag();
channel.basicAck(deliveryTag, removeAllUpTo);
} catch (Exception exception) {
System.out.println("Connection was failed");
continue;
}
}
}
}
}
}
}
提前致谢。