Spring MVC:线程没有在生产中执行

时间:2015-03-10 09:29:54

标签: java multithreading spring spring-mvc tomcat

  • 我正在研究一个我想要使用的Spring-MVC应用程序 线程所以我可以进行数据库事务,发送电子邮件, 发射后的查询速度更快。不幸的是,他们不是 在生产服务器上工作。我放了一个system.out,但事实并非如此 进入线程本身。我有thread.start()。
    • 对于应用程序服务器,我使用的是apache-tomcat。我会张贴 server.xml和我使用该线程的地方。
    • 还有一个问题。我想开始时发出thread.start() 线程。当线程中的任务完成时,线程是否完成 自动死亡和内存被该线程占用?

GroupMemberServiceImpl:

 Thread thread = new Thread(() -> {
        if (!(existingMember == null)) {
            if (!(this.groupMembersDAO.checkIfMemberAlreadyExists(existingMember.getUsername(), groupId))) {
                members.setMemberUsername(members.getMemberUsername().toLowerCase());
                members.setMemberactivated(true);
                createToken(existingMember.getFirstName(),members.getMemberUsername(), members.isAccesslevel(), groupId);
                WaitingMembers waitingMembers = new WaitingMembers();
                waitingMembers.setGroupAccountId(groupId);
                waitingMembers.setMemberEmail(members.getMemberUsername());
                waitingMembers.setNickName(nickName);
                this.waitingMembersService.addMemberInWaiting(waitingMembers);

            }
        } else {
            GroupAccount groupAccount = this.groupAccountService.getGroupById(groupId);
            WaitingMembers waitingMembers = new WaitingMembers();
            waitingMembers.setGroupAccountId(groupId);
            waitingMembers.setMemberEmail(members.getMemberUsername());
            waitingMembers.setNickName(nickName);
            this.waitingMembersService.addMemberInWaiting(waitingMembers);
            Person inviter = this.personService.findPersonByUsername(groupAccount.getAdminUsername());
            sendFirstTimeGroupUserAccountActivationEmail(nickName, members, inviter.getFirstName(), groupAccount.getGroupName(), groupId);

        }
    });
    thread.start();

我使用线程的方式有问题吗?有任何复杂的方法可以防止这个问题吗?

我在生产中使用https,压缩和缓存。

生产中的Server.xml:

 <Connector port="80" protocol="HTTP/1.1" compression="force" compressionMinSize="1024" 
               connectionTimeout="20000"
               redirectPort="443" URIEncoding="utf-8"
 compressableMimeType="text/html,text/xml,text/plain,text/css,text/ javascript,application/x-javascript,application/javascript"/>


<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="200" compression="force"
              compressionMinSize="1024" scheme="https" secure="true" clientAuth="false"  sslProtocol="TLS"
               keystoreFile="/etc/ssl/private/my-keystore.jks" keystorePass="password" URIEncoding="utf-8"
 compressableMimeType="text/html,text/xml,text/plain,text/css,text/ javascript,application/x-javascript,application/javascript"
/>

任何帮助都会很好。非常感谢。

1 个答案:

答案 0 :(得分:0)

最好为spring定义ThreadPoolTask​​Executor。像这样。从here获取代码段。该方法允许控制线程的数量同时工作。

ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) context.getBean("taskExecutor");

PrintTask2 printTask1 = (PrintTask2) context.getBean("printTask2");
printTask1.setName("Thread 1");
taskExecutor.execute(printTask1);

关于你的第二个问题。简短的回答是 - 是的