<commit_before>package com.mrclbndr.jms_demo.commons.adapter.messaging.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mrclbndr.jms_demo.commons.adapter.messaging.api.OrderReceiver;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import java.io.IOException;
import java.util.Optional;

public abstract class AbstractOrderReceiver<OrderType> implements OrderReceiver<OrderType> {
    private static final String PROP_RECEIVER_TIMEOUT = "jms_demo.receiver.timeout";

    @Inject
    private JMSContext jmsContext;

    private ObjectMapper objectMapper;

    @PostConstruct
    public void init() {
        objectMapper = new ObjectMapper();
    }

    @Override
    public Optional<OrderType> nextOrder(Class<OrderType> orderClazz) {
        long timeout = Long.parseLong(System.getProperty(PROP_RECEIVER_TIMEOUT, "500"));

        try (JMSConsumer consumer = createConsumer(jmsContext)) {
            String json = consumer.receiveBody(String.class, timeout);
            if (json != null) {
                OrderType order = objectMapper.readValue(json, orderClazz);
                return Optional.of(order);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    abstract JMSConsumer createConsumer(JMSContext jmsContext);<commit_msg>timeout = 0 means no wait<commit_after>package com.mrclbndr.jms_demo.commons.adapter.messaging.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mrclbndr.jms_demo.commons.adapter.messaging.api.OrderReceiver;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import java.io.IOException;
import java.util.Optional;

public abstract class AbstractOrderReceiver<OrderType> implements OrderReceiver<OrderType> {
    private static final String PROP_RECEIVER_TIMEOUT = "jms_demo.receiver.timeout";

    @Inject
    private JMSContext jmsContext;

    private ObjectMapper objectMapper;

    @PostConstruct
    public void init() {
        objectMapper = new ObjectMapper();
    }

    @Override
    public Optional<OrderType> nextOrder(Class<OrderType> orderClazz) {
        long timeout = Long.parseLong(System.getProperty(PROP_RECEIVER_TIMEOUT, "500"));
        boolean noWait = timeout == 0L;

        try (JMSConsumer consumer = createConsumer(jmsContext)) {
            String json = noWait
                    ? consumer.receiveBodyNoWait(String.class)
                    : consumer.receiveBody(String.class, timeout);
            if (json != null) {
                OrderType order = objectMapper.readValue(json, orderClazz);
                return Optional.of(order);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    abstract JMSConsumer createConsumer(JMSContext jmsContext);
{'fraction_non_alphanumeric': 0.09026845637583893, 'fraction_numerical': 0.0026845637583892616, 'mean_word_length': 3.2585714285714285, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6821843', 'n_tokens_mistral': 849, 'n_tokens_neox': 815, 'n_words': 169}