📄 ordermessageejb.java
字号:
package com.j2eeapp.cdstore.finance;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.jms.*;
import com.j2eeapp.cdstore.util.JNDINames;
import com.j2eeapp.bank.*;
import com.j2eeapp.cdstore.servicelocator.*;
import com.j2eeapp.cdstore.order.*;
public class OrderMessageEJB implements MessageDrivenBean, MessageListener
{
public static final String DRAW="draw";
public static final String DEPOSITE="deposite";
private BankServiceHome bankHome=null;
/**
* Attributes declaration
*/
public MessageDrivenContext EJB_Context = null;
/**
* @J2EE_METHOD -- ejbCreate
* Called by the container to create an instance of a message-driven bean. Each message-driven
* bean class must have one ejbCreate method, with no arguments.
*/
public void ejbCreate () throws ServiceLocatorException
{
try
{
bankHome=(BankServiceHome)new EJBServiceLocator().getRemoteHome(JNDINames.BANK,BankServiceHome.class);
}
catch(ServiceLocatorException ex)
{
throw new ServiceLocatorException("could not get worker: "+ex);
}
}
/**
* @J2EE_METHOD -- onMessage
* Called by the bean's container when a message has arrived for the bean to service.
* It contains the business logic that handles the processing of the message. Only
* message-driven beans can asynchronously receive messages. Session and entity beans
* are not permitted to be JMS MessageListener.
*/
public void onMessage (Message msg)
{
try
{
if(msg instanceof MapMessage)
{
MapMessage map=(MapMessage)msg;
BankService bank=bankHome.findByPrimaryKey(map.getString("cardNumber"));
if(bank==null) return;
if(map.getString("action").equals(DRAW))
{
try
{
boolean re=bank.withdrawFunds(map.getFloat("amountOfMoney"),map.getString("cardPassword"));
if(re)
{
setOrderStatus(map.getString("orderId"),OrderStatusLocalHome.ORDER_STATUS_CONFIRMED);
System.out.println("orderStatus has been set to confirmed");
return;
}
else
{
System.out.println("withdraw money occur error");
return;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}//if
else
{
try
{
boolean re=bank.addFunds(map.getFloat("amountOfMoney"));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}//else
}//if msg
}
catch(Exception e2)
{
System.out.println(e2.getMessage());
}
}
public void setOrderStatus(String orderId,String status)throws ServiceLocatorException
{
try
{
OrderLocalHome orderHome=(OrderLocalHome)new EJBServiceLocator().getLocalHome(JNDINames.ORDER);
OrderLocal order=orderHome.findByPrimaryKey(orderId);
order.getOrderStatus().setStatus(status);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
/**
* @J2EE_METHOD -- ejbRemove
* A container invokes this method before it ends the life of the message-driven object.
* This happens when a container decides to terminate the message-driven object. This
* method is called with no transaction context.
*/
public void ejbRemove ()
{
}
/**
* @J2EE_METHOD -- setMessageDrivenContext
* Set the associated message-driven context. The container calls this method after the
* instance creation. The enterprise Bean instance should store the reference to the
* context object in an instance variable. This method is called with no transaction
* context.
*/
public void setMessageDrivenContext (MessageDrivenContext ctx)
{
this.EJB_Context=ctx;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -