use slf4j logging in more places

This commit is contained in:
2018-05-26 22:56:14 -07:00
parent 583d319ef3
commit 5d1adfc931
4 changed files with 27 additions and 24 deletions

View File

@ -7,6 +7,8 @@ import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HttpsURLConnection;
@ -16,6 +18,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public class OANDAReader implements TickStreamReader {
final Logger log = LoggerFactory.getLogger(OANDAReader.class);
AccountInfo accountInfo;
String[] instruments;
@ -36,15 +39,14 @@ public class OANDAReader implements TickStreamReader {
try {
query = String.format("instruments=%s", URLEncoder.encode(instrList, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("Creating stream URL", e);
}
return String.format("https://stream-%s.oanda.com/v3/accounts/%s/pricing/stream?%s",
type, accountInfo.accountID(), query);
}
private static void readConnection(String urlStr, TickProcessor processor) {
private void readConnection(String urlStr, TickProcessor processor) {
HttpsURLConnection httpConn = null;
String line = null;
try {
@ -65,12 +67,8 @@ public class OANDAReader implements TickStreamReader {
while ((line = is.readLine( )) != null) {
processLine(line, processor);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(SocketTimeoutException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
log.error("Reading OANDA stream", e);
} finally {
httpConn.disconnect();
}
@ -78,21 +76,23 @@ public class OANDAReader implements TickStreamReader {
static ObjectMapper mapper = new ObjectMapper(); // create once, reuse
private static void processLine(String line, TickProcessor processor) {
private void processLine(String line, TickProcessor processor) {
if (line.indexOf ("PRICE") > -1) {
OANDATick tick;
try {
tick = mapper.readValue(line, OANDATick.class);
// log.info(line);
processor.process(tick);
} catch (JsonParseException | JsonMappingException e) {
e.printStackTrace();
log.error("Parsing OANDA data", e);
} catch (IOException e) {
e.printStackTrace();
log.error("Parsing OANDA data", e);
}
} else if (line.indexOf ("HEARTBEAT") > -1) {
// ignore
log.debug(line);
} else {
System.out.println("Unknown type: " + line);
log.warn("Unknown type: {}", line);
}
}
}