elizabeth.morgan0190
elizabeth.morgan0190

Reputation: 33

JMS Message Router - variables (int and double) processing

So I hope i can explain it good it enough, I'm new in JEE and JMS technology.

Currently I'm working on Linux, using Jboss and Wildfly technologies. All described project is saved under: BoldiApp on GitHUb

I am trying to write applications that use the message router pattern. The producer class (Bolid) generates random numbers, sends them to the router class (BolidMonitor), the router class then analyzes the numbers and forwards to the appropriate queue.

The problem I encountered concerns the BolidMonitor class, now the entire String message goes to the BolidMonitor class and I am not able to intercept only int's sent by Bolid to analyse them.

Could anyone help with intercepting by BolidMonitor such values as speed, oil etc.

Thanks in advance and I am sorry if something is not clear enough

Bolid class:

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

    }

}

Upvotes: 1

Views: 306

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 34973

You're sending the data as raw text which you then need to parse in the consumer. You could save yourself the trouble of parsing the text data in the consumer if you sent the message in a more structured way. I recommend creating a javax.jms.MapMessage and the using setInt(String, int) and setDouble(String, double) methods . You could also just set the data as properties on the TextMessage (e.g. using the setIntProperty(String, int) and setDoubleProperty(String, double) methods).

Upvotes: 0

Related Questions