在jboss / wildfly服务器上工作了几个月后,我终于找到了实现此功能的途径:
客户端代码:(基于jboss ejbclient)
package com.mycompany.view.service.wildfly.invocationcontext;import com.mycompany.ejb.internal.MyCompanyAccount;import com.mycompany.view.service.account.LoggedInAccount;import java.util.logging.Level;import java.util.logging.Logger;import javax.annotation.PostConstruct;import javax.ejb.Singleton;import javax.ejb.Startup;import javax.enterprise.inject.Instance;import javax.inject.Inject;import org.jboss.ejb.client.AttachmentKey;import org.jboss.ejb.client.EJBClientContext;import org.jboss.ejb.client.EJBClientInterceptor;import org.jboss.ejb.client.EJBClientInvocationContext;import static com.mycompany.management.wildfly.invocationcontext.MyCompanyInvocationContextKey.MYCOMPANY_ACCOUNT_NUMBER;@Singleton@Startuppublic class MyCompanyInvocationContextInterceptor implements EJBClientInterceptor { private static final Logger LOG = Logger.getLogger(MyCompanyInvocationContextInterceptor.class.getName()); private static final AttachmentKey<Long> MYCOMPANY_ACCOUNT_NUMBER_KEY = new AttachmentKey<>(); @Inject @LoggedInAccount private Instance<MyCompanyAccount> loggedInAccount; @PostConstruct void registerSelf() { EJBClientContext.requireCurrent().registerInterceptor(0, this); } @Override public void handleInvocation(final EJBClientInvocationContext ejbcic) throws Exception { LOG.log(Level.INFO, "Intercepting invocation on: {0}", ejbcic.getInvokedMethod()); final EJBClientContext clientContext = ejbcic.getClientContext(); if (!loggedInAccount.isUnsatisfied()) { final MyCompanyAccount mycompanyAccount = loggedInAccount.get(); if (mycompanyAccount != null) { final Long accountNumber = mycompanyAccount.getAccountNumber(); clientContext.putAttachment(MYCOMPANY_ACCOUNT_NUMBER_KEY, accountNumber); } } ejbcic.getContextData().put(MYCOMPANY_ACCOUNT_NUMBER, "348347878483"); ejbcic.sendRequest(); } @Override public Object handleInvocationResult(final EJBClientInvocationContext ejbcic) throws Exception { return ejbcic.getResult(); }}
在服务器端,我注册了一个全局拦截器:
package com.mycompany.management.wildfly.extension;import com.mycompany.management.facade.account.MyCompanyAccountFacade;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import javax.annotation.Resource;import javax.ejb.EJB;import javax.ejb.EJBContext;import javax.interceptor.AroundInvoke;import javax.interceptor.InvocationContext;public class MyCompanyInvocationContextReceiver { private static final Logger LOG = Logger.getLogger(MyCompanyInvocationContextReceiver.class.getName()); @Resource private EJBContext ejbContext; @EJB private MyCompanyInvocationContext mycompanyInvocationContext; @EJB private MyCompanyAccountFacade mycompanyAccountFacade; @AroundInvoke public Object setMyCompanyAccount(final InvocationContext invocationContext) throws Exception { final Map<String, Object> contextData = ejbContext.getContextData(); LOG.log(Level.INFO, "EJBContext data: {0}", contextData); LOG.log(Level.INFO, "InvocationContext data: {0}", invocationContext.getContextData()); return invocationContext.proceed(); }}
和ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?><ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" version = "3.1" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"> <interceptors> <interceptor> <interceptor-class>com.mycompany.management.wildfly.extension.MyCompanyInvocationContextReceiver</interceptor-class> </interceptor> </interceptors> <assembly-descriptor> <interceptor-binding> <ejb-name>*</ejb-name> <interceptor-class>com.mycompany.management.wildfly.extension.MyCompanyInvocationContextReceiver</interceptor-class> </interceptor-binding> </assembly-descriptor></ejb-jar>
如服务器端拦截器上所示,您可以从InvocationContext或EJBContext获得客户端发送的数据。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)