spring data mongo find方法没有回报

时间:2017-08-19 03:45:23

标签: java spring mongodb

我使用spring scheduler + spring data mongo。

现在我遇到了一个问题.MongoTemple上的find()方法会阻塞。

程序块

List<HistoricalDataModal> historicalList = mongoOperation.find(symbolQuery, HistoricalDataModal.class,"EibHistoricalDataInf");

更新完整代码:

我用findOne替换find,总是阻止 Java代码

  public void realtime() {
          try {
                Query query = new Query();
                List<ContractDetails> contractDetailList = mongoOperation.find(query, ContractDetails.class,"EibContractInf");
                if(contractDetailList == null || contractDetailList.size() == 0) {
                    logger.error("contractdetail error");
                    return;
                }

                for(int index = 0;index < contractDetailList.size();index++) {
                    Query symbolQuery = new Query();
                    symbolQuery.addCriteria(Criteria.where("symbol").is(contractDetailList.get(index).contract().symbol()));
                    FundamentalModal fundamentalDetail = mongoOperation.findOne(symbolQuery, FundamentalModal.class,"EibFundamentalInf");
                    if(fundamentalDetail == null)
                    {
                        logger.error("fundamentalDetail error");
                        continue;
                    }
                    logger.debug("debug1");

                    logger.debug("debug");

                    /*List<*/HistoricalDataModal/*>*/ historicalList = mongoOperation.findOne(symbolQuery, HistoricalDataModal.class,"EibHistoricalDataInf");
                    logger.debug("debug");
                    if(historicalList == null /*|| historicalList.size() == 0*/) {
                        logger.error("historicalList error");
                        continue;
                    }
                    logger.debug("debug");

                }

          }catch (Exception e) {
            // TODO: handle exception
        }
      }

日志输出: Getting element by ID in Ember

如果我将HistoricalDataModal替换为FundamentalModal,程序就不会阻止。因为它存在HistoricalDataModal问题。但我不知道原因。

2 个答案:

答案 0 :(得分:0)

Bar.java

package com.ib.client;

public class Bar {

    private String m_time;
    private double m_open;
    private double m_high;
    private double m_low;
    private double m_close;
    private long m_volume;
    private int m_count;
    private double m_wap;

    public Bar(String time, double open, double high, double low, double close, long volume, int count, double wap) {
        this.m_time = time;
        this.m_open = open;
        this.m_high = high;
        this.m_low = low;
        this.m_close = close;
        this.m_volume = volume;
        this.m_count = count;
        this.m_wap = wap;
    }

    public String time() {
        return m_time;
    }

    public double open() {
        return m_open;
    }

    public double high() {
        return m_high;
    }

    public double low() {
        return m_low;
    }

    public double close() {
        return m_close;
    }

    public long volume() {
        return m_volume;
    }

    public int count() {
        return m_count;
    }

    public double wap() {
        return m_wap;
    }

}

HistoricalDataModal.java

package pro.elijah.batch.modal;

import com.ib.client.Bar;

public class HistoricalDataModal {

    private String symbol;
    private Bar bar;
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public Bar getBar() {
        return bar;
    }
    public void setBar(Bar bar) {
        this.bar = bar;
    }
    @Override
    public String toString() {
        return "HistoricalDataModal [symbol=" + symbol + ", bar=" + bar + "]";
    }



}

答案 1 :(得分:0)

我正在分享一个如何验证Bar类是否正确映射到Mongo文档的示例。

例如,它是来自mongo shell的historicalDataModal个对象的集合:

> db.getCollection("historicalDataModal").find()
{ "_id" : ObjectId("599eba80ee19f50d0ceecb07"), "_class" : "com.mongodb.demo.mongodbdemo.model.HistoricalDataModal", "symbol" : "xiix", "bar" : { "m_time" : "1234", "m_open" : 23, "m_high" : 34, "m_low" : 45, "m_close" : 56, "m_volume" : NumberLong(23), "m_count" : 2, "m_wap" : 34.5 } }
{ "_id" : ObjectId("599ebaacee19f50d56141eb7"), "_class" : "com.mongodb.demo.mongodbdemo.model.HistoricalDataModal", "symbol" : "xiix", "bar" : { "m_time" : "1234", "m_open" : 23, "m_high" : 34, "m_low" : 45, "m_close" : 56, "m_volume" : NumberLong(23), "m_count" : 2, "m_wap" : 34.5 } }
{ "_id" : ObjectId("599ebab1ee19f50d932f8c29"), "_class" : "com.mongodb.demo.mongodbdemo.model.HistoricalDataModal", "symbol" : "xiix", "bar" : { "m_time" : "1234", "m_open" : 23, "m_high" : 34, "m_low" : 45, "m_close" : 56, "m_volume" : NumberLong(23), "m_count" : 2, "m_wap" : 34.5 } }
> 

正如您所看到的,Bar对象的属性以m_开头,例如m_timem_openm_high。实际上Bar类具有带该前缀的属性。但构造函数没有相同的前缀,实际上Bar构造函数有timeopenhigh没有m_前缀。

为了查看此类行为的替代方法,请尝试使用以下Bar类,您将看到构造函数与Bar类的成员具有相同的前缀

选项1:没有构造函数的Bar类

public class Bar {

    private String m_time;
    private double m_open;
    private double m_high;
    private double m_low;
    private double m_close;
    private long m_volume;
    private int m_count;
    private double m_wap;


    public String time() {
        return m_time;
    }

    public double open() {
        return m_open;
    }

    public double high() {
        return m_high;
    }

    public double low() {
        return m_low;
    }

    public double close() {
        return m_close;
    }

    public long volume() {
        return m_volume;
    }

    public int count() {
        return m_count;
    }

    public double wap() {
        return m_wap;
    }

}

选项2具有相同前缀名称的构造函数的Bar类

public class Bar {

    private String m_time;
    private double m_open;
    private double m_high;
    private double m_low;
    private double m_close;
    private long m_volume;
    private int m_count;
    private double m_wap;

    public Bar(String m_time, double m_open, double m_high, double m_low, double m_close, long m_volume, int m_count, double m_wap) {
        this.m_time = m_time;
        this.m_open = m_open;
        this.m_high = m_high;
        this.m_low = m_low;
        this.m_close = m_close;
        this.m_volume = m_volume;
        this.m_count = m_count;
        this.m_wap = m_wap;
    }

    public String time() {
        return m_time;
    }

    public double open() {
        return m_open;
    }

    public double high() {
        return m_high;
    }

    public double low() {
        return m_low;
    }

    public double close() {
        return m_close;
    }

    public long volume() {
        return m_volume;
    }

    public int count() {
        return m_count;
    }

    public double wap() {
        return m_wap;
    }

}