我写了一个示例远程ejb客户端连接到远程服务器wildfly 9.0.2 final, 在这里我的代码:
<?php
class Class_demo
{
public function validate()
{
$err = array();
if(empty($_POST['first_name']))
{
$err['first_name'] = 'first name is required!';
}
if(empty($_POST['middle_name']))
{
$err['middle_name'] = 'middle name is required!';
}
if(empty($_POST['last_name']))
{
$err['last_name'] = 'last name is required';
}
if(!empty($err))
{
return $err;
}
}
}
if(isset($_POST['submit_form']))
{
$class = new Class_demo();
$response_msg = $class->validate();
echo "<pre>";
if(!empty($response_msg['first_name']))
{
print_r($response_msg['first_name'] ."<br/>");
}
if(!empty($response_msg['middle_name']))
{
print_r( $response_msg['middle_name'] ."<br/>");
}
if(!empty($response_msg['last_name']))
{
print_r($response_msg['last_name']);
}
if(empty($response_msg))
{
print_r('Success');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SOME FORM</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="first_name">
<span id="first_empty"></span> <!-- Return first name is required! -->
<input type="text" name="middle_name">
<span id="middle_empty"></span> <!-- Return middle name is required! -->
<input type="text" name="last_name">
<span id="last_empty"></span> <!-- Return last name is required! -->
<input type="submit" value="Submit" name="submit_form">
</form>
</body>
</html>
这是服务器端standalone-full.xml的重要部分:
public class EjbRemoteClient {
public static void main(String[] args) {
// Connection to Wildfly Server instance
String host = "remote ip server";
String port = "7273"; // Wildfly HTTP port
Context remotingContext;
try {
remotingContext = createRemoteEjbContext(host, port);
} catch (NamingException e) {
System.err.println("Error setting up remoting context");
e.printStackTrace();
return;
}
String ejbUrl = "ejb:/myserver_core/someinterfaceImpl!com.art.dataserver.api.ejb.inf.BulkRemoteInf";
someinterface mtnBulk;
try {
mtnBulk = createEjbProxy(remotingContext, ejbUrl, someinterface .class);
} catch (NamingException e) {
System.err.println("Error resolving bean");
e.printStackTrace();
return;
} catch (ClassCastException e) {
System.err.println("Resolved EJB is of wrong type");
e.printStackTrace();
return;
}
// Call remote method with parameter
try {
AimedJobsModel aimedJobsModel =mtnBulk.getAimedJobs();
} catch (Exception e) {
System.err.println("Error accessing remote bean");
e.printStackTrace();
return;
}
}
/**
* Create Remote EJB Context.
*
* @return remote EJB context
* @throws NamingException
* if creating the context fails
*/
private static Context createRemoteEjbContext(String host, String port) throws NamingException {
Hashtable<Object, Object> props = new Hashtable<>();
props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
props.put("jboss.naming.client.ejb.context", false);
props.put("org.jboss.ejb.client.scoped.context", true);
//props.put("endpoint.name", "client-endpoint");
props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false);
props.put("remote.connections", "default");
props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false);
props.put(Context.SECURITY_PRINCIPAL, "myusername");
props.put(Context.SECURITY_CREDENTIALS, "mypass");
props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port);
props.put("remote.connection.default.host", host);
props.put("remote.connection.default.port", port);
props.put("remote.connection.default.connect.timeout", "15000");
return new InitialContext(props);
}
/**
* Get a proxy for a remote EJB.
*
* @param remotingContext
* remote EJB context
* @param ejbUrl
* URL of the EJB
* @param ejbInterfaceClass
* class of the remote interface of the EJB
* @param <T>
* type of the EJB remote interface
* @return EJB proxy
* @throws NamingException
* if the name resolving fails
* @throws ClassCastException
* if the EJB proxy is not of the given type
*/
@SuppressWarnings("unchecked")
private static <T> T createEjbProxy(Context remotingContext, String ejbUrl, Class<T> ejbInterfaceClass)
throws NamingException, ClassCastException {
Object resolvedproxy = remotingContext.lookup(ejbUrl);
return (T) resolvedproxy;
}}
我的例外是:
<subsystem xmlns="urn:jboss:domain:remoting:3.0">
<endpoint/>
<http-connector name="http-remoting-connector" connector-ref="default" security-realm="ApplicationRealm"/>
<outbound-connections>
<remote-outbound-connection name="remote-ejb-connection" outbound-socket-binding-ref="remote-ejb" username="myusername" security-realm="ejb-security-realm">
<properties>
<property name="SASL_POLICY_NOANONYMOUS" value="false"/>
<property name="SSL_ENABLED" value="false"/>
</properties>
</remote-outbound-connection>
</outbound-connections>
</subsystem>