Closed xtuyaowu closed 5 years ago
Hi xtuyaowu,
Sorry for the delay in responding to your query. In order to use the library you will need to have an Acceptor running (you can use the AppServer in the examples).
Once it's running try the following test:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import io.allune.quickfixj.spring.boot.starter.EnableQuickFixJClient;
import io.allune.quickfixj.spring.boot.starter.examples.client.ClientApplicationAdapter;
import quickfix.Application;
import quickfix.ConfigError;
import quickfix.LogFactory;
import quickfix.MessageFactory;
import quickfix.MessageStoreFactory;
import quickfix.Session;
import quickfix.SessionSettings;
import quickfix.ThreadedSocketInitiator;
import quickfix.field.ClOrdID;
import quickfix.field.OrigClOrdID;
import quickfix.field.Side;
import quickfix.field.Symbol;
import quickfix.field.Text;
/**
* Created by Administrator on 2019-8-12 0012.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ClientApplicationAdapterTest.ClientAppConfig.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class ClientApplicationAdapterTest {
@EnableQuickFixJClient
public static class ClientAppConfig {
@Bean
public Application clientApplication() {
return new ClientApplicationAdapter();
}
@Bean
public ThreadedSocketInitiator clientInitiator(quickfix.Application clientApplication, MessageStoreFactory clientMessageStoreFactory,
SessionSettings clientSessionSettings, LogFactory clientLogFactory,
MessageFactory clientMessageFactory) throws ConfigError {
return new ThreadedSocketInitiator(clientApplication, clientMessageStoreFactory, clientSessionSettings,
clientLogFactory, clientMessageFactory);
}
}
@Autowired
private ThreadedSocketInitiator clientInitiator;
@Test
public void sendToTarget() throws Exception {
quickfix.fix41.OrderCancelRequest message = new quickfix.fix41.OrderCancelRequest(
new OrigClOrdID("123"),
new ClOrdID("321"),
new Symbol("LNUX"),
new Side(Side.BUY));
message.set(new Text("Cancel My Order!"));
Session session = clientInitiator.getManagedSessions().get(0);
while (!session.isLoggedOn()) {
System.out.println("Waiting for login success");
Thread.sleep(500);
}
session.send(message);
System.out.println("run to ");
}
}
Thanks for reply esanchezros 😊. This is test cases you have shown. Same changes can you accommodate into the source code as well so than instead of running it as junit test case, I can run it as normal java application.
Thank, Barun
Barun, all you have to do is @Autowire
the clientInitiator
in your app and send messages, right?
Hi esanchezros, How about below changes to our App Client.java class. package io.allune.quickfixj.spring.boot.starter.examples.client;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean;
import io.allune.quickfixj.spring.boot.starter.EnableQuickFixJClient; import lombok.extern.slf4j.Slf4j; import quickfix.Application; import quickfix.ConfigError; import quickfix.Initiator; import quickfix.LogFactory; import quickfix.MessageFactory; import quickfix.MessageStoreFactory; import quickfix.SessionSettings; import quickfix.ThreadedSocketInitiator;
@Slf4j @EnableQuickFixJClient @SpringBootApplication public class AppClient implements CommandLineRunner { @Autowired private ThreadedSocketInitiator initiator; public static void main(String[] args) { SpringApplication.run(AppClient.class, args); }
@Override
public void run(String... args) throws Exception {
quickfix.fix41.OrderCancelRequest message = new OrigClOrdID("123"),
new ClOrdID("321"), new Symbol("LNUX"), new Side(Side.BUY));
message.set(new Text("Cancel My Order!"));
Session session = initiator.getManagedSessions().get(0);
while (!session.isLoggedOn()) {
System.out.println("Waiting for login success");
Thread.sleep(500);
}
session.send(message);
System.out.println("run to ");
log.info("Joining thread, you can press Ctrl+C to shutdown application");
Thread.currentThread().join();
}
@Bean
public Application clientApplication() {
return new ClientApplicationAdapter();
}
@Bean
public ThreadedSocketInitiator clientInitiator(quickfix.Application clientApplication, MessageStoreFactory clientMessageStoreFactory,
SessionSettings clientSessionSettings, LogFactory clientLogFactory,
MessageFactory clientMessageFactory) throws ConfigError {
return new ThreadedSocketInitiator(clientApplication, clientMessageStoreFactory, clientSessionSettings,
clientLogFactory, clientMessageFactory);
}
}
Barun, the examples provided illustrate simple ways to make use of the QuickFixJ Spring Boot starter. You can use the EnableQuickFixJClient
and EnableQuickFixJServer
for other types of apps. Normally you receive orders from clients via a portal or third party integrations and then send them to the server. Likewise, when the server has processed the order, it sends responses to the client. The run
method here it's just purely to keep the client running while is connected to the server. Your business logic should live elsewhere in your project
hi How does client send messages to the server,the below code seem wrong? thanks!
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import quickfix.Message; import quickfix.Session; import quickfix.SessionID; import quickfix.field.*;
import static org.junit.Assert.*;
/**
Created by Administrator on 2019-8-12 0012. */ @RunWith(SpringRunner.class) @SpringBootTest(classes = AppClient.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @EnableAutoConfiguration public class ClientApplicationAdapterTest {
@Autowired private ClientApplicationAdapter clientApplicationAdapter;
@Test public void sendToTarget() throws Exception { quickfix.fix41.OrderCancelRequest message = new quickfix.fix41.OrderCancelRequest( new OrigClOrdID("123"), new ClOrdID("321"), new Symbol("LNUX"), new Side(Side.BUY));
}
}