我有一个@Stateless @Local Bean成功部署在耳朵里.当我浏览JNDI树时,我可以看到新的EJB 3.1标准全局JNDI名称.(JAVA的:全球/产品/产品EJB /产品经理)
我想在同一个应用服务器上的不同应用程序中使用此EJB .我是否需要为此EJB添加远程接口?
当我尝试在托管bean中验证来自我的JSF的用户输入时,我遇到了一些问题.我在控制台中收到了验证消息,但我没有在页面中看到它.我不明白问题出在哪里.
这是控制台输出:
信息:内部验证方法!! 信息:没有比赛!信息:实例化org.hibernate.validator.engine.resolver.JPATraversableResolver的实例.信息:警告:FacesMessage已入队,但可能尚未显示.的SourceID = bRegForm:名称[严重性=(INFO 0),摘要=(您的名字不能包含特殊字符),细节=(您的名字不能包含特殊字符)]
这就是我制作JSF输入组件的方式:
<h:inputText id="name" value="#{registrationController.name}" validator="#{registrationController.validateName}" required="true">
<h:message for="name"/>
</h:inputText>
Run Code Online (Sandbox Code Playgroud)
这就是我如何创建验证方法:
@ManagedBean
@RequestScoped
public class RegistrationController {
...
public void validateName(FacesContext context, UIComponent validate,
Object value) {
String inputFromField = (String) value;
String simpleTextPatternText = "^[a-zA-Z0-9]+$";
Pattern textPattern = null;
Matcher nameMatcher = null;
textPattern = Pattern.compile(simpleTextPatternText);
nameMatcher = textPattern.matcher(inputFromField);
System.out.println("Inside validation method!!");
if (!nameMatcher.matches()) {
System.out.println("NO MATCH!!!");
FacesMessage msg = new FacesMessage(
Your name cannot contain special characters);
throw new ValidatorException(msg);
}
} …Run Code Online (Sandbox Code Playgroud) 我们在从应用程序调用存储过程时遇到问题.数据库是oracle 10g
这个proc有2个输入参数和2个输出参数.
输入1: - DB-List输入2: - 字符串
输出1: - 更新DB-List输出2: - 数字
当我们尝试使用时
Query q = session.createSQLQuery("{call proc_name(?,?,?,?)}");
Run Code Online (Sandbox Code Playgroud)
我们无法区分in参数和out参数.那么我们应该如何使用它来处理它.
另外,我们尝试使用可调用语句如下:
Session session = (Session) getEntityManager().getDelegate();
SessionImpl sessionImpl = ((SessionImpl) getEntityManager().getDelegate());
Connection cc = sessionImpl.connection();
CallableStatement callableStatement = null;
callableStatement = cc.prepareCall("{call proc_name(?,?,?,?)}");
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("DB_LIST",callableStatement.getConnection());
ARRAY paramArray = new ARRAY(descriptor, callableStatement.getConnection(), array);
callableStatement.setArray(1, paramArray);
callableStatement.setString(2, "N");
callableStatement.registerOutParameter(3, OracleTypes.ARRAY, "DB_RETURN_LIST");
callableStatement.registerOutParameter(4, Types.INTEGER);
// executing the query
callableStatement.execute();
Run Code Online (Sandbox Code Playgroud)
我们收到以下错误:
javax.ejb.EJBException: java.lang.ClassCastException:
$Proxy50 cannot be cast …Run Code Online (Sandbox Code Playgroud) 我有以下代码在Apache-tomee上运行,我使用eclipse进行编码,我想在应用程序启动时使用@Schedule注释运行我的简单调度作业.
@Startup
@Singleton
public class ScheduleEJB {
private static int count = 0;
@Schedule(second="*/10", minute="*", hour="*", info="MyTimer")
public void execute() {
System.out.println("its running count..."+count);
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在Tomee上部署此代码时,它会让我听到以下消息并且无法在启动时自动运行我的调度方法execute(),因为此代码在glassfish中工作正常,但我不会使用它或者是jboss的glassfish.
2012年12月21日上午9:59:45 org.quartz.impl.StdSchedulerFactory实例化
INFO:使用ThreadExecutor的默认实现
2012年12月21日上午9:59:45 org.quartz.core.SchedulerSignalerImpl
INFO:类型的初始化调度程序信号器: class org.quartz.core.SchedulerSignalerImpl
2012年12月21日上午9:59:45 org.quartz.core.QuartzScheduler
信息:Quartz Scheduler v.2.1.6创建.
2012年12月21日上午9:59:45 org.quartz.simpl.RAMJobStore初始化
INFO:RAMJobStore已初始化.
2012年12月21日上午9:59:45 org.quartz.core.QuartzScheduler初始化
INFO:调度程序元数据:Quartz调度程序(v2.1.6)'OpenEJB-TimerService-Scheduler'与instanceId'OpenEJB'
调度程序类:'org. quartz.core.QuartzScheduler' - 在本地运行.
没有开始.
目前处于待机模式.
执行的作业数:0
使用线程池'org.apache.openejb.core.timer.DefaultTimerThreadPoolAdapter' - 带0个线程.
使用job-store'org.quartz.simpl.RAMJobStore' - 它不支持持久性.并没有聚集.
2012年12月21日上午9:59:45 org.quartz.impl.StdSchedulerFactory实例化
INFO:从外部提供的属性实例初始化的Quartz调度程序'OpenEJB-TimerService-Scheduler'.
2012年12月21日上午9:59:45 org.quartz.impl.StdSchedulerFactory实例化
INFO:Quartz调度程序版本:2.1.6
2012年12月21日上午9:59:45 org.quartz.core.QuartzScheduler启动
INFO:Scheduler OpenEJB- TimerService-Scheduler _ $ _ OpenEJB启动.
2012年12月21日上午9:59:45 org.apache.openejb.assembler.classic.Assembler createApplication INFO:创建Ejb …
假设我有一个定义两个视图的EJB:
两个接口共享相同的方法签名,因此它类似于:
public interface MyBusinessCommon {
void myMethod(Object o);
}
@Local
public interface MyBusinessLocal extends MyBusinessCommon { }
@Remote
public interface MyBusinessRemote extends MyBusinessCommon { }
@Stateless
public class MyBusinessBean implements MyBusinessLocal, MyBusinessRemote {
public void myMethod(Object o) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法弄清楚EJB本身(或其拦截器?)内部调用的EJB视图?
假设我想根据使用的视图执行不同的授权程序.远程应该更加受限制,本地不应该.
我可以调用SessionContext#getInvokedBusinessInterface()但这只给了我关于类对象的信息 - 而不是关于它的EJB语义.明确地使用反射来检查接口或bean上的注释是否存在是不够的(在ejb-jar.xml?中定义的视图怎么样?)
我怀疑是否有可能使用直接的EJB规范,但也许我错过了一些东西.
如果没有,是否可以从应用程序服务器的内部获取此信息?(我们只考虑JBoss AS 7.x,Glassfish 3.x和TomEE 1.5.1).
有没有办法创建一个拦截器限定符注释,忽略注释字符串值以进行限定?
例如:
Log.java
@Inherited
@InterceptorBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default ""; // <---- ignore this
}
Run Code Online (Sandbox Code Playgroud)
LogInterceptor.java
@Log
@Interceptor
public class LogInterceptor implements Serializable {
...
}
Run Code Online (Sandbox Code Playgroud)
Usage.java
@Log("message for this log")
public String interceptedMethod(String param) {
...
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为注释value("message for this log")作为限定符,但我想使用value()not作为限定符,但使用消息日志.
我有一个@AroundInvoke REST Web服务拦截器,我想用它来记录常见数据,如类和方法,远程IP地址和响应时间.
使用InvocationContext获取类和方法名称很简单,只要被拦截的Rest服务在其参数列表中包含@Context HttpServletRequest,就可以通过HttpServletRequest获得远程IP.
但是有些REST方法的参数中没有HttpServletRequest,在这些情况下我无法弄清楚如何获取HttpServletRequest对象.
例如,以下REST Web服务没有@Context HttpServletRequest参数
@Inject
@Default
private MemberManager memberManager;
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Member add(NewMember member) throws MemberInvalidException {
return memberManager.add(member);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试将它直接注入我的拦截器,但是(在JBoss 6.1上)它总是为空...
public class RestLoggedInterceptorImpl implements Serializable {
@Context
HttpServletRequest req;
@AroundInvoke
public Object aroundInvoke(InvocationContext ic) throws Exception {
logger.info(req.getRemoteAddr()); // <- this throws NPE as req is always null
...
return ic.proceed();
Run Code Online (Sandbox Code Playgroud)
我想建议一种可靠的方法来访问HttpServletRequest对象 - 甚至只是Http Headers ...无论REST服务是否包含参数.
任何人都可以解释在Java EE 6应用程序中实现Singleton的完整过程吗?我假设我不应该以声明静态变量的典型方式创建单例并且应该使用@Singleton注释?我必须这样做吗?
这仅仅是一个声明它的情况@Singleton,就是这样吗?我还要上课了吗?
那么我需要做什么才能访问其他类中的单例?
我在@Resource注释的两个属性之间感到困惑.
Java Documentations说:
mappedName:此资源应映射到的产品特定名称.由name元素或defaultaulted定义的此资源的名称是使用资源的应用程序组件的本地名称.(它是JNDI java:comp/env命名空间中的名称.)许多应用程序服务器提供了将这些本地名称映射到应用程序服务器已知资源名称的方法.此映射名称通常是全局JNDI名称,但可以是任何表单的名称.
lookup:引用指向的资源的名称.它可以使用全局JNDI名称链接到任何兼容的资源.
我的问题是如何根据什么标准在mappedName和lookup之间进行选择?
我创建了一个自定义注释,如下所示
@InterceptorBinding
@Retention(RUNTIME)
@Target(TYPE, METHOD)
public @interface Traceable {}
Run Code Online (Sandbox Code Playgroud)
我写了一个拦截器,如下所示
@Traceable
@Interceptor
public class EnterExitLogger {
@AroundInvoke
public Object aroundInvoke(InvocatiobContext c) {}
}
Run Code Online (Sandbox Code Playgroud)
拦截器和注释位于名为common-utils的模块中.
我在类级别用@Traceable注释了我的目标类,如下所示
@Traceable
public class CDIManagedBean {
}
Run Code Online (Sandbox Code Playgroud)
我在beans.xml文件中声明了拦截器条目,如下所示
<interceptors>
<class>my.package.EnterExitLogger</class>
</interceptors>
Run Code Online (Sandbox Code Playgroud)
目标类位于单独的模块中.beans.xml位于目标类模块的META-INF目录中.
目标类的方法是从rest类调用的.当我调用方法时,不会调用拦截器的AroundInvoke方法.
我阅读了文档,并了解拦截器应该包含一个公共的无参数构造函数.我加了.但仍然没有调用拦截器.
我在阅读完文档后在自定义注释中添加了@Inherited.但仍然没有调用拦截器.
从文档中我注意到拦截器实现了Serializable接口.虽然没有提到我也实现了Serializable.仍然没有奏效.
然后我从拦截器,beans.xml文件和目标类中删除了自定义注释.我还从拦截器中删除了public no参数构造函数并删除了Serializable.
然后我用目标类注释@Interceptors(EnterExitLogger.class)并调用了流程.我的拦截器被调用了.
任何人都可以告诉我如何处理InterceptorBinding?
PS
我正在WAS 8.5服务器上部署我的耳朵.
java-ee-6 ×10
java ×5
java-ee ×5
ejb-3.1 ×4
ejb ×3
interceptor ×3
jboss-weld ×2
annotations ×1
apache-tomee ×1
cdi ×1
ejb-jar.xml ×1
jax-rs ×1
jboss ×1
jndi ×1
jpa ×1
jsf ×1
jsf-2 ×1
qualifiers ×1
schedule ×1
singleton ×1
websphere-8 ×1