📄 sendpasswordbean.java
字号:
/*
* Created on 2004-7-3
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.study.exercise;
import javax.ejb.MessageDrivenBean;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
/**
* @ejb.bean name="SendPassword"
* acknowledge-mode="Auto-acknowledge"
* destination-type="javax.jms.Topic"
* subscription-durability="NonDurable"
* transaction-type="Bean"
*
*--
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.bean ejb-name="SendPassword"
* jndi-name="SendPasswordBean"
*
*--
**/
public class SendPasswordBean implements MessageDrivenBean, MessageListener {
/** Required method for container to set context. */
public void setMessageDrivenContext(
javax.ejb.MessageDrivenContext messageContext)
throws javax.ejb.EJBException {
this.messageContext = messageContext;
}
/**
* Required creation method for message-driven beans.
*
* @ejb.create-method
*/
public void ejbCreate() {
// no specific action required for message-driven beans
}
/** Required removal method for message-driven beans. */
public void ejbRemove() {
messageContext = null;
}
/**
* This method implements the business logic for the EJB.
*
* <p>Make sure that the business logic accounts for asynchronous message processing.
* For example, it cannot be assumed that the EJB receives messages in the order they were
* sent by the client. Instance pooling within the container means that messages are not
* received or processed in a sequential order, although individual onMessage() calls to
* a given message-driven bean instance are serialized.
*
* <p>The <code>onMessage()</code> method is required, and must take a single parameter
* of type javax.jms.Message. The throws clause (if used) must not include an application
* exception. Must not be declared as final or static.
*/
public void onMessage(javax.jms.Message message) {
System.out.println("Message Driven Bean got message " + message);
// do business logic here
TextMessage tm=(TextMessage)message;
try{
//从消息中获取用户ID
System.out.println("get Text " + tm.getText());
String id = tm.getText();
//获取密码
InitialContext ctx=new InitialContext();
Object objRef = ctx.lookup("UserBean");
UserHome
home=(UserHome)javax.rmi.PortableRemoteObject.narrow(
objRef,UserHome.class);
User bean =home.findByPrimaryKey(id);
String ps=bean.getPassword();
String email_address=bean.getEmail_address();
//发送邮件
String host ="127.0.0.1" ;
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", host);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("master@hyl.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ( email_address));
newMessage.setSubject("password");
newMessage.setSentDate(new Date());
String content=new String("you pasword is: ");
newMessage.setText(content+ps);
Transport.send(newMessage);
} catch (Exception e){
System.out.println(e.toString());
}
}
/** The context for the message-driven bean, set by the EJB container. */
private javax.ejb.MessageDrivenContext messageContext = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -