1、下载CTP接口完整包(包含编译好的DLL和java工程)和安装swigwin-2.0.11,配置好swigwin系统环境变量
2、解压,演示Win 64位的做法
3、在当前文件夹里面新建src 和 ctp 文件夹,在ctp文件夹里面新建thosttraderapi文件夹
4、在当前文件夹里面新建 various.i 和 thostapi.i 文件
thostapi.i文件内容
%module(directors="1") thosttraderapi
%include "various.i"
%apply char **STRING_ARRAY { char *ppInstrumentID[] }
%{
#include "ThostFtdcMdApi.h"
#include "ThostFtdcTraderApi.h"
%}
%feature("director") CThostFtdcMdSpi;
%include "ThostFtdcUserApiDataType.h"
%include "ThostFtdcUserApiStruct.h"
%include "ThostFtdcMdApi.h"
%feature("director") CThostFtdcTraderSpi;
%include "ThostFtdcTraderApi.h"
various.i 文件内容
/*
* char **STRING_ARRAY typemaps.
* These typemaps are for C String arrays which are NULL terminated.
* char *values[] = { "one", "two", "three", NULL }; // note NULL
* char ** is mapped to a Java String[].
*
* Example usage wrapping:
* %apply char **STRING_ARRAY { char **input };
* char ** foo(char **input);
*
* Java usage:
* String numbers[] = { "one", "two", "three" };
* String[] ret = modulename.foo( numbers };
*/
%typemap(jni) char **STRING_ARRAY "jobjectArray"
%typemap(jtype) char **STRING_ARRAY "String[]"
%typemap(jstype) char **STRING_ARRAY "String[]"
%typemap(in) char **STRING_ARRAY (jint size) {
int i = 0;
size = JCALL1(GetArrayLength, jenv, $input);
#ifdef __cplusplus
$1 = new char*[size+1];
#else
$1 = (char **)calloc(size+1, sizeof(char *));
#endif
for (i = 0; i<size; i++) {
jstring j_string = (jstring)JCALL2(GetObjectArrayElement, jenv, $input, i);
const char *c_string = JCALL2(GetStringUTFChars, jenv, j_string, 0);
#ifdef __cplusplus
$1[i] = new char [strlen(c_string)+1];
#else
$1[i] = (char *)calloc(strlen(c_string)+1, sizeof(const char *));
#endif
strcpy($1[i], c_string);
JCALL2(ReleaseStringUTFChars, jenv, j_string, c_string);
JCALL1(DeleteLocalRef, jenv, j_string);
}
$1[i] = 0;
}
%typemap(freearg) char **STRING_ARRAY {
int i;
for (i=0; i<size$argnum-1; i++)
#ifdef __cplusplus
delete[] $1[i];
delete[] $1;
#else
free($1[i]);
free($1);
#endif
}
%typemap(out) char **STRING_ARRAY {
int i;
int len=0;
jstring temp_string;
const jclass clazz = JCALL1(FindClass, jenv, "java/lang/String");
while ($1[len]) len++;
jresult = JCALL3(NewObjectArray, jenv, len, clazz, NULL);
/* exception checking omitted */
for (i=0; i<len; i++) {
temp_string = JCALL1(NewStringUTF, jenv, *result++);
JCALL3(SetObjectArrayElement, jenv, jresult, i, temp_string);
JCALL1(DeleteLocalRef, jenv, temp_string);
}
}
%typemap(javain) char **STRING_ARRAY "$javainput"
%typemap(javaout) char **STRING_ARRAY {
return $jnicall;
}
/*
* char **STRING_OUT typemaps.
* These are typemaps for returning strings when using a C char ** parameter type.
* The returned string appears in the 1st element of the passed in Java String array.
*
* Example usage wrapping:
* void foo(char **string_out);
*
* Java usage:
* String stringOutArray[] = { "" };
* modulename.foo(stringOutArray);
* System.out.println( stringOutArray[0] );
*/
%typemap(jni) char **STRING_OUT "jobjectArray"
%typemap(jtype) char **STRING_OUT "String[]"
%typemap(jstype) char **STRING_OUT "String[]"
%typemap(javain) char **STRING_OUT "$javainput"
%typemap(in) char **STRING_OUT($*1_ltype temp) {
if (!$input) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
return $null;
}
if (JCALL1(GetArrayLength, jenv, $input) == 0) {
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
return $null;
}
$1 = &temp;
}
%typemap(argout) char **STRING_OUT {
jstring jnewstring = NULL;
if($1) {
jnewstring = JCALL1(NewStringUTF, jenv, *$1);
}
JCALL3(SetObjectArrayElement, jenv, $input, 0, jnewstring);
}
/*
* char *BYTE typemaps.
* These are input typemaps for mapping a Java byte[] array to a C char array.
* Note that as a Java array is used and thus passeed by reference, the C routine
* can return data to Java via the parameter.
*
* Example usage wrapping:
* void foo(char *array);
*
* Java usage:
* byte b[] = new byte[20];
* modulename.foo(b);
*/
%typemap(jni) char *BYTE "jbyteArray"
%typemap(jtype) char *BYTE "byte[]"
%typemap(jstype) char *BYTE "byte[]"
%typemap(in) char *BYTE {
$1 = (char *) JCALL2(GetByteArrayElements, jenv, $input, 0);
}
%typemap(argout) char *BYTE {
JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0);
}
%typemap(javain) char *BYTE "$javainput"
/* Prevent default freearg typemap from being used */
%typemap(freearg) char *BYTE ""
5、当前文件夹 Shift+右键打开命令行窗口,执行
swig.exe -c++ -java -package ctp.thosttraderapi -outdir src -o thosttraderapi_wrap.cpp thostapi.i
将会生成thosttraderapi_wrap.cpp 和thosttraderapi_wrap.h
6、进入src目录,执行javac *.java
cd src
javac *.java
7、把src中所有的.class文件复制到ctp的thosttraderapi文件夹中
8、回到解压目录执行jar cf thosttraderapi.jar ctp 命令
cd ..
jar cf thosttraderapi.jar ctp
会生成 thosttraderapi.jar 包
9、打开VS2013
新建-》项目-》WIN32项目-》DLL
在源文件中加入下面的八个文件,在thosttraderapi_wrap.cpp第一行加入#include "stdafx.h"
调试,把错误的方法注释掉。最后生成thosttraderapi_wrap.dll
包最后生成的thosttraderapi_wrap.dll 、thosttraderapi.dll、thostmduserapi.dll 放到 %JAVA_HOME%/bin 目录下面。
新建一个spring boot 工程
application.properties
ctp.MdAddress=tcp://180.168.146.187:10010
ctp.instruementids=v1901,cu1811
ctp.BrokerId=9999
ctp.UserId=127630
ctp.PassWord=123456
package com.ctp.thosttraderapi;
import ctp.thosttraderapi.*;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MdspiImpl extends CThostFtdcMdSpi{
private String m_BrokerId;
private String m_UserId;
private String m_PassWord;
public String instruementids;//合约代码
public CThostFtdcMdApi m_mdapi;
public MdspiImpl(CThostFtdcMdApi mdapi){
m_mdapi = mdapi;
}
@Override
public void OnFrontConnected(){
System.out.println("On Front Connected");
CThostFtdcReqUserLoginField field = new CThostFtdcReqUserLoginField();
field.setBrokerID(m_BrokerId);
field.setUserID(m_UserId);
field.setPassword(m_PassWord);
m_mdapi.ReqUserLogin(field, 1);
}
@Override
public void OnRspUserLogin(CThostFtdcRspUserLoginField pRspUserLogin, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
if (pRspUserLogin != null) {
System.out.printf("Brokerid[%s]\n",pRspUserLogin.getBrokerID()+",登陆成功"+pRspUserLogin.getUserID()+","+nRequestID);
}
if (pRspInfo.getErrorID()!=0){
System.out.println(pRspInfo.getErrorID()+","+pRspInfo.getErrorMsg());
}
int i = m_mdapi.SubscribeMarketData(instruementids.trim().split(","),instruementids.trim().split(",").length);
if (i==0) System.out.println("订阅成功");
System.out.println("交易日"+this.m_mdapi.GetTradingDay());
}
@Override
public void OnRspSubMarketData(CThostFtdcSpecificInstrumentField pSpecificInstrument, CThostFtdcRspInfoField RspInfo, int nRequestID, boolean bIsLast){
if (RspInfo.getErrorID()!=0){
System.out.println(pSpecificInstrument.getInstrumentID());
System.out.println(RspInfo.getErrorID()+","+RspInfo.getErrorMsg());
}
super.OnRspSubMarketData(pSpecificInstrument,RspInfo,nRequestID,bIsLast);
}
@Override
public void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField pSpecificInstrument, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
System.out.println("OnRspUnSubMarketData");
super.OnRspUnSubMarketData(pSpecificInstrument, pRspInfo, nRequestID, bIsLast);
}
@Override
public void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField pDepthMarketData) {
if (pDepthMarketData != null)
{
System.out.printf("合约代码[%s]最新价[%f]时间[%s]\n",pDepthMarketData.getInstrumentID(),pDepthMarketData.getLastPrice(),pDepthMarketData.getTradingDay()+" "+pDepthMarketData.getUpdateTime()+":"+pDepthMarketData.getUpdateMillisec());
}
else
{
System.out.printf("NULL obj\n");
}
}
@Override
public void OnFrontDisconnected(int nReason) {
/*
如果客户端到行情前置的无身份验证连接建立失败,这个函数被调用。其中的参数说明连接失败的原因
*/
super.OnFrontDisconnected(nReason);
System.out.println("连接失败!!!" + nReason);
}
/**
* 如果交易系统无法识别客户端发送的请求消息,就通过这个函数返回错误信息
* @param pRspInfo
* @param nRequestID
* @param bIsLast
*/
@Override
public void OnRspError(CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
System.out.println("OnRspError");
super.OnRspError(pRspInfo, nRequestID, bIsLast);
}
/**
* 如果超过一定时间在客户端和系统之间没有任何消息交换发生,这个函数会发送心跳用来说明客户端到系统服务器之间的连接是活跃的。
目前此接口已经不再起效
* @param nTimeLapse
*/
@Override
public void OnHeartBeatWarning(int nTimeLapse) {
System.out.println("OnHeartBeatWarning");
super.OnHeartBeatWarning(nTimeLapse);
}
}
package com.ctp.thosttraderapi;
import ctp.thosttraderapi.CThostFtdcMdApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class ThostmdUserStart implements ApplicationRunner {
static{
System.out.println("java.library.path:"+System.getProperty("java.library.path"));
System.loadLibrary("thostmduserapi");
System.loadLibrary("thosttraderapi");
System.loadLibrary("thosttraderapi_wrap");
}
@Value("${ctp.MdAddress}")
private String ctp1_MdAddress;//前置机地址
@Value("${ctp.BrokerId}")
private String m_BrokerId;
@Value("${ctp.UserId}")
private String m_UserId;
@Value("${ctp.PassWord}")
private String m_PassWord;
@Value("${ctp.instruementids}")
public String instruementids;//合约代码
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
CThostFtdcMdApi mdApi = CThostFtdcMdApi.CreateFtdcMdApi();
MdspiImpl pMdspiImpl = new MdspiImpl(mdApi);
pMdspiImpl.setInstruementids(instruementids);
pMdspiImpl.setM_BrokerId(m_BrokerId);
pMdspiImpl.setM_UserId(m_UserId);
pMdspiImpl.setM_PassWord(m_PassWord);
mdApi.RegisterSpi(pMdspiImpl);
mdApi.RegisterFront(ctp1_MdAddress);
mdApi.Init();
mdApi.Join();
}
}
启动程序
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2018-10-12 13:59:56.724 INFO 16672 --- [ main] com.ctp.ThosttraderapiApplication : Starting ThosttraderapiApplication on XTZ-20180628ENV with PID 16672 (E:\GitCode\Payment\ThostTraderAPI\target\classes started by Administrator in E:\GitCode\Payment\ThostTraderAPI)
2018-10-12 13:59:56.826 INFO 16672 --- [ main] com.ctp.ThosttraderapiApplication : No active profile set, falling back to default profiles: default
2018-10-12 13:59:56.857 INFO 16672 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1d548a08: startup date [Fri Oct 12 13:59:56 CST 2018]; root of context hierarchy
2018-10-12 13:59:58.719 INFO 16672 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-10-12 13:59:58.728 INFO 16672 --- [ main] com.ctp.ThosttraderapiApplication : Started ThosttraderapiApplication in 2.376 seconds (JVM running for 3.318)
On Front Connected
Brokerid[,登陆成功,0]
订阅成功
交易日20181012
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:07:0]
合约代码[cu1811]最新价[50650.000000]时间[20181012 14:00:06:500]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:07:500]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:08:0]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:08:0]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:08:500]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:09:0]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:09:0]
合约代码[v1901]最新价[6710.000000]时间[20181012 14:00:09:500]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:10:0]
合约代码[v1901]最新价[6710.000000]时间[20181012 14:00:10:0]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:10:500]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:10:500]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:11:500]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:12:0]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:12:500]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:12:500]
合约代码[v1901]最新价[6715.000000]时间[20181012 14:00:13:0]
合约代码[cu1811]最新价[50660.000000]时间[20181012 14:00:13:500]
合约代码[v1901]最新价[6710.000000]时间[20181012 14:00:13:500]
合约代码[v1901]最新价[6710.000000]时间[20181012 14:00:14:0]
合约代码[v1901]最新价[6710.000000]时间[20181012 14:00:15:0]