我正在使用spring和hibernate并希望发送Async Mail但它不适用于我的情况。我从很多人的想法中读到了很多,但这些解决方案并不适用于我。
包com.kayoner.hesk.daos;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Future;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
@Component
public class SendMail {
public static Logger logger=Logger.getLogger(SendMail.class);
public SendMail() {
}
/**
* @param host
* @param port
* @param senderEmail
* @param passwordParam
* @param receiverEmail
* @param subject
* @param content
* @return
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
@Async
public void sendMailGeneral(String host, String port, String senderEmail,
String passwordParam, String receiverEmail, String replyTo,String subject,
String content) throws MessagingException,
UnsupportedEncodingException {
try {
logger.info("THREAD SLEEP FOR 3000 MS");
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param input
* @param variables
* @return
* @throws Exception
*/
public String parseTemplates(String input, Map<String, Object> variables)
throws Exception {
Velocity.init();
VelocityContext vc = new VelocityContext();
if (variables != null) {
Iterator<Map.Entry<String, Object>> itr =
variables.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Object> entry = itr.next();
String key = entry.getKey();
Object value = entry.getValue();
if (key != null) {
vc.put(key, value);
}
}
}
StringWriter output = new StringWriter();
Velocity.evaluate(vc, output, "LOG", input);
return output.toString();
}
}
弹簧servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
">
<context:property-placeholder
location="classpath:jdbc.properties,classpath:domain.properties" />
<context:component-scan base-package="com.kayoner" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<mvc:resources mapping="/images/**" location="/images/,/images/dashboard/" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
<task:scheduler id="scheduler" pool-size="10" />
<task:executor id="executor" queue-capacity="100" />
<util:map id="datasources">
<entry key="tenant1" value-ref="dataSource" />
<!-- <entry key="tenant2" value-ref="dataSource2"/> -->
<!-- <entry key="company" value-ref="company"/> -->
</util:map>
<bean id="datasourcesCls" class="com.kayoner.multytanecy.DatasourcesCls">
<property name="datasources" ref="datasources"></property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="validator" class="org.springmodules.validation.bean.BeanValidator" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<!-- This is for the server side validation for the data -->
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager"></mvc:annotation-driven>
<bean id="webSessionTenantIdentifierResolver"
class="com.kayoner.multytanecy.WebSessionCurrentTenantIdentifierResolver"
scope="request">
<aop:scoped-proxy />
</bean>
<bean id="simpleTenantConnectionProvider"
class="com.kayoner.multytanecy.SimpleMultiTenantConnectionProvider">
<property name="dataSourceMap" ref="datasources" />
</bean>
<bean id="sessionFactory" class="com.kayoner.multytanecy.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="dataSourceMap" ref="datasources" />
<property name="annotatedClasses">
<list>
<value>com.kayoner.hesk.models.Tickets</value>
<value>com.kayoner.hesk.models.Replies</value>
</list>
</property>
<property name="hibernateProperties">
<map>
<entry key="hibernate.dialect" value="${hibernate.dialect}"></entry>
<entry key="hibernate.show_sql" value="true"></entry>
<entry key="hibernate.multiTenancy" value="DATABASE" />
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.tenant_identifier_resolver" value-ref="webSessionTenantIdentifierResolver" />
<entry key="hibernate.multi_tenant_connection_provider"
value-ref="simpleTenantConnectionProvider" />
</map>
</property>
<property name="mappingResources">
<list>
<value>hibernate.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="autodetectDataSource" value="false" />
</bean>
<bean id="customizationSettingsDAO"
class="com.kayoner.companysettings.companysettingsdaos.CustomizationSettingsDAO"></bean>
<bean id="customizationSettingsService"
class="com.kayoner.companysettings.companysettingsserviceImpl.CustomizationSettingsServiceImpl"></bean>
<!-- <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes"> <map> <entry key="html" value="text/html" />
<entry key="json" value="application/json" /> </map> </property> <property
name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="true" /> </bean> </list> </property> </bean>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
</bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters"> <list> <ref bean="jacksonMessageConverter"
/> </list> </property> </bean> -->
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
<property name="defaultContentType" value="application/json" />
</bean>
<!-- bind your messages.properties -->
<!-- <bean class="org.springframework.context.support.ResourceBundleMessageSource"
id="messageSource">
<property name="basename" value="messages" />
</bean> -->
<!-- Configure the multipart resolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="200000000" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
<property name="cookieName" value="myAppLocaleCookie"></property>
<property name="cookieMaxAge" value="3600"></property>
</bean>
<!-- <bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean> -->
<security:global-method-security
jsr250-annotations="enabled" pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler" />
</security:global-method-security>
<bean id="webExpressionHandler"
class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />
<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="permissionEvaluator" />
</bean>
<bean id="user" class="com.kayoner.usermanage.usermodel.User">
<aop:scoped-proxy />
</bean>
<bean id="permissionEvaluator" class="com.kayoner.security.BasePermissionEvaluator">
<!-- <property name="user" ref="user"></property> -->
<aop:scoped-proxy />
</bean>
<bean id="domainSettings" class="com.kayoner.multytanecy.DomainSettings"></bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Kayoner-Helpdesk</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://www.springframework.org/schema/beans</taglib-uri>
<taglib-location>
/WEB-INF/tld/spring-beans-3.0.xsd
</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.springframework.org/schema/context</taglib-uri>
<taglib-location>/WEB-INF/tld/spring-context-3.0.xsd</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.springframework.org/schema/tx</taglib-uri>
<taglib-location>
/WEB-INF/tld/spring-tx-3.0.xsd
</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.springframework.org/schema/task</taglib-uri>
<taglib-location>
/WEB-INF/tld/spring-task-3.0.xsd
</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://www.springframework.org/schema/mvc</taglib-uri>
<taglib-location>
/WEB-INF/tld/spring-mvc-3.0.xsd
</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://tiles.apache.org/dtds</taglib-uri>
<taglib-location>
/WEB-INF/lib/tiles-config_2_0.dtd
</taglib-location>
</taglib>
</jsp-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.kayoner.Sessions.SessionListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml,
/WEB-INF/tiles-defs.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<session-config>
<session-timeout>3600</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/hesk/errors/errorCode404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/jsp/hesk/errors/errorCode403.jsp</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/WEB-INF/jsp/hesk/errors/errorCode403.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/WEB-INF/jsp/hesk/errors/errorCode405.jsp</location>
</error-page>
<error-page>
<exception-type>org.springframework.transaction.CannotCreateTransactionException</exception-type>
<location>/WEB-INF/jsp/hesk/errors/errorCode505.jsp</location>
</error-page>
<error-page>
<exception-type>org.hibernate.exception.JDBCConnectionException</exception-type>
<location>/WEB-INF/jsp/hesk/errors/errorCode505.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.ClassFormatError</exception-type>
<location>/WEB-INF/jsp/hesk/errors/catchError.jsp</location>
</error-page>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>com.kayoner.multytanecy.CustomSpringSecurityFilterChain</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- <filter>
<filter-name>HeaderFilter</filter-name>
<filter-class>com.kayoner.multytanecy.HeaderFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HeaderFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> -->
</web-app>
请帮我解决这个问题?