所以我希望我能解释得足够好,我是JEE和JMS技术的新手。
目前,我正在使用Jboss和Wildfly技术开发Linux。所有描述的项目都保存在以下位置:BoldiApp on GitHUb
我正在尝试编写使用消息路由器模式的应用程序。生产者类(Bolid)生成随机数,将其发送到路由器类(BolidMonitor),然后路由器类分析这些数字并将其转发到适当的队列。
我遇到的问题与BolidMonitor类有关,现在整个String消息都转到BolidMonitor类,而我无法仅截获Bolid发送来分析它们的int。
有人可以帮助BolidMonitor拦截速度,机油等值吗?
先谢谢您,如果有一些不清楚的地方,我感到抱歉
Bolid类:
@JMSDestinationDefinitions(value = {
@JMSDestinationDefinition(name = "java:/queue/myQueue", interfaceName = "javax.jms.Queue", destinationName = "BolidMonitor"),
@JMSDestinationDefinition(name = "java:/topic/myTopic", interfaceName = "javax.jms.Topic", destinationName = "BolidLogger") })
@WebServlet(urlPatterns = "/")
public class Bolid extends HttpServlet {
private static final long serialVersionUID = 3234027581994367438L;
public static double getRandomIntegerBetweenRange(double min, double max) {
double x = (int) (Math.random() * ((max - min) + 1)) + min;
return x;
}
@EJB
MessangeProducer producer;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int speed = (int) getRandomIntegerBetweenRange(0, 350);
int oil = (int) getRandomIntegerBetweenRange(300, 400);
double tire = getRandomIntegerBetweenRange(1.7, 2.2);
String data = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()).toString();
String messange = data + "\nVehicle speed: " + speed + " km/h" + "\nOil pressure is: " + oil + " kPa"
+ "" + "\nTire pressure is : " + tire + " bar" + "/n";
producer.sendMessange2(messange);
producer.sendMessange(messange);
resp.getWriter().write("Message from bolid: " + messange);
}
private Map<Long, Bolid> categoryMap;
public void updateCategoryMap(Integer startPosition, Integer maxResult) {
categoryMap = new HashMap<Long, Bolid>();
int speed = (int) getRandomIntegerBetweenRange(0, 350);
int oil = (int) getRandomIntegerBetweenRange(300, 400);
double tire = getRandomIntegerBetweenRange(1.7, 2.2);
String data = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()).toString();
String messange = data + "\nVehicle speed: " + speed + " km/h" + "\nOil pressure is: " + oil + " kPa"
+ "" + "\nTire pressure is : " + tire + " bar" + "/n";
producer.sendMessange2(messange);
producer.sendMessange(messange);
}
}
BolidMonitor:
@MessageDriven(name = "BolidMonitor", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "queue/myQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
public class BolidMonitor implements MessageListener {
private static Logger LOGGER = Logger.getLogger(BolidMonitor.class.toString());
public void onMessage(Message messange) {
if (messange instanceof TextMessage) {
try {
String text = ((TextMessage) messange).getText();
LOGGER.info("Message send to BolidMonitor from Bolid : " + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
您正在将数据作为原始text发送,然后需要在使用者中进行解析。如果您以更结构化的方式发送消息,则可以避免在使用者中解析文本数据的麻烦。我建议创建javax.jms.MapMessage
并使用javax.jms.MapMessage
和setInt(String, int)
方法。您也可以仅在setInt(String, int)
上将数据设置为setDouble(String, double)
(例如,使用setDouble(String, double)
和properties方法)。