java - 如何从 src 文件夹访问 Hibernate session ?

标签 java hibernate grails

我想知道如何在 src/java 文件夹中的示例类中正确访问服务和域

public class NewsIngestion implements Runnable {

private String str;
private int num;
private Logger log = Logger.getLogger("grails.app");
private static boolean isRunning;
private Thread t;
private WorkerJobService jobService;
private NewsService newsService;

public NewsIngestion(String s, int n)
{
    jobService = new WorkerJobService();
    newsService = new NewsService();

    str = s;
    num = n;
    isRunning = false;

    t = new Thread (this, "NewsIngestion");

}

public void run ()
{

    while(isRunning){
        try{
            if(jobService.isJobEnabled("ConsumeFeedsJob") && jobService.lockJob("ConsumeFeedsJob")){
                log.info("${this.class.name}: ConsumeFeedsJob started");

                try{
                    // get all sources
                    List sources = (List) InvokerHelper.invokeMethod(RSSFeed.class, "list", null);

                    for(int i = 0; i < sources.size(); i++) {

                        RSSFeed s = (RSSFeed) sources.get(i);

                        // check if it's time to read the source
                        int diff = DateTimeUtil.getSecondsDateDiff(s.getLastChecked(), new Date());

                        if(s.getLastChecked() == null || diff >= s.getCheckInterval()){

                            List keyword_list = (List) InvokerHelper.invokeMethod(Keyword.class, "list", null);

                            for(int j = 0; j < keyword_list.size(); j++) {

                                String keyword = (String) keyword_list.get(j);

                                try{
                                    newsService.ingestNewsFromSources(keyword, s);
                                }catch(Exception e){
                                    log.error("${this.class.name}: ${e}");
                                }

                                log.debug("Completed reading feeds for ${keyword}.");
                                log.info("${this.class.name}: Reading feeds for '${keyword}' (${s.feedName}) took ${Float.toString(st2.getDuration())} second(s).");
                            }

                            s.setLastChecked(new Date());
                            InvokerHelper.invokeMethod(RSSFeed.class, "save", null);
                        }

                        log.info("${this.class.name}: Reading feeds for '${s.feedName}' for all keywords took ${Float.toString(st.getDuration())} second(s).");
                    }

                }catch(Exception e){
                    log.error("${this.class.name}: Exception: ${e}");
                }

                log.info("${this.class.name}: ConsumeFeedsJob ended.");

                // unlock job
                jobService.unlockJob("ConsumeFeedsJob");
            }

            log.info("alfred: success");

        }
        catch (Exception e){
            log.info("alfred exception: " + e.getMessage());
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            log.info(e.getMessage());
        }
    }
}

public void start() {
    if(t == null){
        t = new Thread (this, "NewsIngestion");
    }

    if(!isRunning){
        isRunning = true;
        t.start();
    }
}

public void stop() {
    isRunning = false;
}

public boolean isRunning() {
    return isRunning;
}

}

我遇到此错误消息:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

谢谢。

最佳答案

您不应该自己实例化服务类,而应该从主上下文中获取类实例

import org.codehaus.groovy.grails.commons.ApplicationHolder

def ctx = ApplicationHolder.application.mainContext
def newsService = ctx.newsService

如果您使用的是 Java

import org.codehaus.groovy.grails.commons.ApplicationHolder

public class SomeClass  {
   SomeService someService;
   public SomeClass()  {
       someService = (SomeService) ApplicationHolder.getApplication().getMainContext().getBean("someService");
   }
}

关于java - 如何从 src 文件夹访问 Hibernate session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4713729/

相关文章:

java - 如何解释 Java 线程堆栈?

java - 如何检查 int 是否包含字母

java - 使用 JTable 和列按钮从数据库中删除正确的行时出现问题

java - 如何在java中将度分秒转换为小数

java - 使用 Hibernate 将 Oracle 日期映射到 Java 对象

java - spring mvc Hibernate 搜索查询

java - 如何提高我的软件项目的速度?

grails - Grails异步集成测试

grails - 在GGTS中找不到配置VisualVM

grails 2.0 包含资源的简单方法?