Search in sources :

Example 1 with Session

use of quickfix.Session in project camel by apache.

the class TradeExecutor method sendMessage.

private void sendMessage(SessionID sessionID, Message message) {
    try {
        Session session = Session.lookupSession(sessionID);
        if (session == null) {
            throw new SessionNotFound(sessionID.toString());
        }
        DataDictionaryProvider provider = session.getDataDictionaryProvider();
        if (provider != null) {
            try {
                ApplVerID applVerID = getApplVerID(session, message);
                DataDictionary appDataDictionary = provider.getApplicationDataDictionary(applVerID);
                appDataDictionary.validate(message, true);
            } catch (Exception e) {
                LogUtil.logThrowable(sessionID, "Outgoing message failed validation: " + e.getMessage(), e);
                return;
            }
        }
        for (QuickfixjMessageListener listener : listeners) {
            try {
                listener.onMessage(sessionID, message);
            } catch (Throwable e) {
                LogUtil.logThrowable(sessionID, "Error while dispatching message", e);
            }
        }
    } catch (SessionNotFound e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : ApplVerID(quickfix.field.ApplVerID) DataDictionary(quickfix.DataDictionary) DataDictionaryProvider(quickfix.DataDictionaryProvider) Session(quickfix.Session) SessionNotFound(quickfix.SessionNotFound)

Example 2 with Session

use of quickfix.Session in project camel by apache.

the class QuickfixjConsumer method sendOutMessage.

private void sendOutMessage(Exchange exchange) throws QFJException {
    Message camelMessage = exchange.getOut();
    quickfix.Message quickfixjMessage = camelMessage.getBody(quickfix.Message.class);
    log.debug("Sending FIX message reply: {}", quickfixjMessage);
    SessionID messageSessionID = exchange.getIn().getHeader("SessionID", SessionID.class);
    Session session = getSession(messageSessionID);
    if (session == null) {
        throw new IllegalStateException("Unknown session: " + messageSessionID);
    }
    if (!session.send(quickfixjMessage)) {
        throw new CannotSendException("Could not send FIX message reply: " + quickfixjMessage.toString());
    }
}
Also used : Message(org.apache.camel.Message) SessionID(quickfix.SessionID) Session(quickfix.Session)

Example 3 with Session

use of quickfix.Session in project camel by apache.

the class QuickfixjProducer method sendMessage.

void sendMessage(Exchange exchange, org.apache.camel.Message camelMessage) throws Exception {
    Message message = camelMessage.getBody(Message.class);
    log.debug("Sending FIX message: {}", message);
    SessionID messageSessionID = getEndpoint().getSessionID();
    if (messageSessionID == null) {
        messageSessionID = MessageUtils.getSessionID(message);
    }
    Session session = getSession(messageSessionID);
    if (session == null) {
        throw new IllegalStateException("Unknown session: " + messageSessionID);
    }
    Callable<Message> callable = null;
    if (exchange.getPattern().isOutCapable()) {
        MessageCorrelator messageCorrelator = getEndpoint().getEngine().getMessageCorrelator();
        callable = messageCorrelator.getReply(getEndpoint().getSessionID(), exchange);
    }
    if (!session.send(message)) {
        throw new CannotSendException("Cannot send FIX message: " + message.toString());
    }
    if (callable != null) {
        Message reply = callable.call();
        exchange.getOut().getHeaders().putAll(camelMessage.getHeaders());
        exchange.getOut().setBody(reply);
    }
}
Also used : Message(quickfix.Message) SessionID(quickfix.SessionID) Session(quickfix.Session)

Example 4 with Session

use of quickfix.Session in project camel by apache.

the class QuickfixjProducerTest method processInOutExchangeSendUnsuccessful.

@Test
public void processInOutExchangeSendUnsuccessful() throws Exception {
    Mockito.when(mockExchange.getPattern()).thenReturn(ExchangePattern.InOut);
    Mockito.when(mockExchange.getProperty(QuickfixjProducer.CORRELATION_CRITERIA_KEY)).thenReturn(new MessagePredicate(sessionID, MsgType.EMAIL));
    Mockito.when(mockExchange.getProperty(QuickfixjProducer.CORRELATION_TIMEOUT_KEY, 1000L, Long.class)).thenReturn(5000L);
    org.apache.camel.Message mockOutboundCamelMessage = Mockito.mock(org.apache.camel.Message.class);
    Mockito.when(mockExchange.getOut()).thenReturn(mockOutboundCamelMessage);
    final Message outboundFixMessage = new Email();
    outboundFixMessage.getHeader().setString(SenderCompID.FIELD, "TARGET");
    outboundFixMessage.getHeader().setString(TargetCompID.FIELD, "SENDER");
    Session mockSession = Mockito.spy(TestSupport.createSession(sessionID));
    Mockito.doReturn(mockSession).when(producer).getSession(MessageUtils.getSessionID(inboundFixMessage));
    Mockito.doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        quickfixjEngine.getMessageCorrelator().onEvent(QuickfixjEventCategory.AppMessageReceived, sessionID, outboundFixMessage);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }, 10);
            return false;
        }
    }).when(mockSession).send(Matchers.isA(Message.class));
    producer.process(mockExchange);
    Mockito.verify(mockOutboundCamelMessage, Mockito.never()).setBody(Matchers.isA(Message.class));
    Mockito.verify(mockSession).send(inboundFixMessage);
    Mockito.verify(mockExchange).setException(Matchers.isA(CannotSendException.class));
}
Also used : Email(quickfix.fix42.Email) Message(quickfix.Message) IOException(java.io.IOException) JMException(javax.management.JMException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Session(quickfix.Session) Test(org.junit.Test)

Example 5 with Session

use of quickfix.Session in project camel by apache.

the class QuickfixjProducerTest method setExceptionOnExchange.

@Test
public void setExceptionOnExchange() throws Exception {
    Session mockSession = Mockito.spy(TestSupport.createSession(sessionID));
    Mockito.doReturn(mockSession).when(producer).getSession(MessageUtils.getSessionID(inboundFixMessage));
    Mockito.doThrow(new TestException()).when(mockSession).send(Matchers.isA(Message.class));
    producer.process(mockExchange);
    Mockito.verify(mockExchange).setException(Matchers.isA(TestException.class));
}
Also used : Message(quickfix.Message) Session(quickfix.Session) Test(org.junit.Test)

Aggregations

Session (quickfix.Session)14 Message (quickfix.Message)9 SessionID (quickfix.SessionID)9 Test (org.junit.Test)6 DataDictionary (quickfix.DataDictionary)6 IOException (java.io.IOException)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 JMException (javax.management.JMException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Email (quickfix.fix42.Email)2 Message (org.apache.camel.Message)1 DataDictionaryProvider (quickfix.DataDictionaryProvider)1 SessionNotFound (quickfix.SessionNotFound)1 ApplVerID (quickfix.field.ApplVerID)1