1

所以我希望我能很好地解释它,我是 JEE 和 JMS 技术的新手。

目前我在 Linux 上工作,使用 Jboss 和 Wildfly 技术。所有描述的项目都保存在:Github 上的 BoldiApp

我正在尝试编写使用消息路由器模式的应用程序。生产者类(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();
            }
        }

    }

}
4

1 回答 1

0

您将数据作为原始文本发送,然后需要在消费者中解析。如果您以更结构化的方式发送消息,您可以省去在消费者中解析文本数据的麻烦。我建议创建一个javax.jms.MapMessage和使用setInt(String, int)setDouble(String, double)方法。您也可以将数据设置为属性TextMessage例如使用setIntProperty(String, int)andsetDoubleProperty(String, double)方法)。

于 2020-04-24T15:27:33.630 回答