Samstag, 30. November 2013

JBoss AS 7 programmatic EJB Remote Client

Recently I have worked on a project which I had to migrate from JBoss 6 to JBoss 7 and the main problem for me was the EJB remote client connection to the JBoss 7.

An EJB remote client with JBoss 7 can be connected with the following three different approaches.
  1. jboss-ejb-client.properties.
  2. Setting property file through system property jboss.ejb.client.properties.file.path
  3. programmatically
The first two approaches can be found on the jboss documentation (https://docs.jboss.org/author/display/AS72/EJB+invocations+from+a+remote+client+using+JNDI) and in the jboss quickstarts. The problems with those are:
  • static nature
  • useless in case of client specific security (username, password)
The solution for the last approach was very hard to find and time consuming. So here is my solution to make it work with a JBoss specific JBossClientContextBuilder. (Note: Do not forget the jboss-client.jar in your classpath).

IHelloBean.java
public interface IHelloBean {
 String getHello();
}
HelloBean.java
@Stateless
@Remote(IHelloBean.class)
public class HelloBean implements IHelloBean
{
 @Override
 public String getHello()
 {
  return "Hello";
 }
}
HelloClient.java
public class HelloClient
{
 public static void main(String[] args) throws NamingException
 {
  final Context context = new JBossClientContextBuilder().build();

  String lookupString = "ejb:/sectest/HelloBean!test.IHelloBean";
  final IHelloBean helloBean = (IHelloBean) context.lookup(lookupString);
  System.out.println("Hello: " + helloBean.getHello());
 }
}
JBossClientContextBuilder.java
public class JBossClientContextBuilder {
 private String port;
 private String host;
 private String user;
 private String password;
 private boolean noanonymous;

 public JBossClientContextBuilder port(String port) {
  this.port = port;
  return this;
 }

 public JBossClientContextBuilder host(String host) {
  this.host = host;
  return this;
 }

 public JBossClientContextBuilder user(String user) {
  this.user = user;
  return this;
 }

 public JBossClientContextBuilder password(String password) {
  this.password = password;
  return this;
 }

 public JBossClientContextBuilder noanonymous(boolean noanonymous) {
  this.noanonymous = noanonymous;
  return this;
 }

 public Context build() throws NamingException {
  Properties clientProp = new Properties();
  clientProp
    .put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
      "false");
  clientProp.put("remote.connections", "default");

  // Default 4447
  clientProp.put("remote.connection.default.port", StringUtils.defaultIfEmpty(port, "4447"));

  // Default 127.0.0.1
  clientProp.put("remote.connection.default.host",
    StringUtils.defaultIfEmpty(host, "127.0.0.1"));

  if (!StringUtils.isEmpty(user)) {
   clientProp.put("remote.connection.default.username", user);
  }
  if (!StringUtils.isEmpty(password)) {
   clientProp.put("remote.connection.default.password", password);
  }

  if (noanonymous) {
   clientProp
     .put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",
       "true");
   clientProp
     .put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS",
       "JBOSS-LOCAL-USER");
  }
  clientProp
    .put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT",
      "false");

  System.out.println(Arrays.deepToString(clientProp.entrySet().toArray(
    new Map.Entry[0])));

  EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(
    clientProp);
  ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(
    cc);
  EJBClientContext.setSelector(selector);

  Properties props = new Properties();
  props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

  return new InitialContext(props);
 }
}


14 Kommentare:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    AntwortenLöschen
  2. Dieser Kommentar wurde vom Autor entfernt.

    AntwortenLöschen
  3. I really appreciate the information shared above. It’s of great help. If someone wants to learn Online (Virtual) instructor lead live training in JBOSS, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor-led training on JBOSS. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ pieces of training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain, and UAE etc.
    saurabh
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:(0) 8553576305
    http://www.maxmunus.com/

    AntwortenLöschen
  4. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    AntwortenLöschen
  5. I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
    I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!



    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    AntwortenLöschen
  6. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    python Training in Chennai
    python Training in Bangalore
    python Training in Pune

    AntwortenLöschen
  7. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    AntwortenLöschen

  8. I am looking for and I love to post a comment Python classes in punethat "The content of your post is awesome" Great work!

    AntwortenLöschen
  9. A good first place to search educational web template - TemplateMonster. You can find there free templates.

    AntwortenLöschen
  10. such a great word which you use in your article and article is amazing knowledge. thank you for sharing it.

    Looking for Software Training in Bangalore , learn from Softgen Infotech Software Courses on online training and classroom training. Join today!

    AntwortenLöschen
  11. I really like looking through an blog article that can make people think. Also, many thanks for allowing for me to comment!

    AntwortenLöschen
  12. I must say you are very much concise and experienced at persuasive writing. I just loved your flair of writing.
    Data Science training in Mumbai
    Data Science course in Mumbai
    SAP training in Mumbai

    AntwortenLöschen