Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Open URL in EJB

Options
  • 17-04-2005 7:08pm
    #1
    Registered Users Posts: 6,315 ✭✭✭


    I have a Stateful Session Bean called MessageManagerBean. This has a method called addMessage which adds a new message to the database.

    What I want to do is each time a new message is added to the database call a URL which physically sends the message via a seperate server. This URL returns a line of text indicating success or error.

    Now in PHP this is quite straightforward:
    $status = file($url);

    What I'm wondering is how to do this in my Stateful Session Bean or is this possible at all?

    Any help greatly appreciated. Ii have tried numerous solutions over the last two days, all of which were complex and didn't work.


Comments

  • Closed Accounts Posts: 92 ✭✭tempest


    URL Class

    You'll should add a resource-ref to the deployment descriptor, and lookup via JNDI.
        <resource-ref>
            <description>A name of a URL. This URL will then be configured in the application server at Deploy Time</description>
            <res-ref-name>url/URLName</res-ref-name>
            <res-type>java.net.url</res-type>
            <res-auth>Container</res-auth>
        </resource-ref>
    
    InitialContext ic = new InitialContext();
    URL url = (URL)ic.lookup("url/URLName");
    


  • Closed Accounts Posts: 92 ✭✭tempest


    I completely misunderstood what you were saying there. (I think)

    Presumably this is some kind of a http url which can read from the same database and is able to send the message for you.

    Can you give more details about your particular restrictions?

    Why is the application on a separate server?
    Does it have to be this way?
    If so can you modify the other server? The most appropriate way may be to expose the application as a web service.
    If you can't modify the other server then you can use the URL classes to create a connection to the URL but it's not a nice way to do it if there are other options available

    Clearz, in his infinite wisdom, has put some code up which performs the kind of operations you want in this thread which should help you to get it working.


  • Registered Users Posts: 6,315 ✭✭✭ballooba


    The URL connects to a third party server to send a SMS message. I do not have controll over this.

    Another option available to me is to contact that server by email.

    It has been suggested to me that it is bad design to have this in the Stateful Session Bean. Where should I put it?

    I understand Message Driven Beans are only for email? I would prefer the URL method a it gives delivery report access etc.


  • Closed Accounts Posts: 92 ✭✭tempest


    ballooba wrote:
    The URL connects to a third party server to send a SMS message. I do not have controll over this.
    Well that settles that then. If the only way to send the message is via the third party server then that pretty much closes all windows except this one.
    ballooba wrote:
    It has been suggested to me that it is bad design to have this in the Stateful Session Bean. Where should I put it?
    It's bad design in general to use Stateful Session Beans. Can you use a Stateless Session Bean instead? Why do you require the stateful bean?
    Stateful Beans impose a large resource usage overhead on the container. They can in certain circumstances be very useful, but in general they offer little that cannot be achieved in a better performing way using other techniques. If you cite why you are using I may propose alternatives.

    ballooba wrote:
    I understand Message Driven Beans are only for email? I would prefer the URL method a it gives delivery report access etc.

    Message Driven Beans are not only for email, although I doubt that they are what you are looking for. Message Driven Beans are used in situations where the appropriate action is different to that of normal processing. MDB's are a queuing technology. When you post a message to a queue control returns immediately to you. Sometime later, maybe immediately the message is picked up off the queue by the MDB and processed. (In J2EE we use MDB's but the technology is far more prevalent than that.)

    In order to attack your problem. Have a look at Clearz' code which manipulates the O2 site. The code is quite clear (no Pun intended) and achieves along the lines of what you need to be able to do.


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    You can use the Apache HttpClient library to "talk" to the sms server. It should accomplish what you need to do.


  • Advertisement
  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Tried the HttpClient but having problem's importing the org.apache.commons package into my environment. Don't know how to basically. Tried to add it to the build path of the project but that did not work.

    Even then, I don't think I should be calling be callinf the HttpClient from my SFSB?


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Make sure it is on the classpath.

    Why dont you think you should call it in your sfsb? Its the only place you can call it from unless you do it in your jsp/servlet


  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Ok, I have put the commons package in the folder referred to as Classpath in my Environment Variables.

    This code does nothing. Sometimes when I deploy it I get a MessageManagerBean not Bound error. The code calling the method is as follows:
    String status = sendMessage(sender,recipient,text)
    
    And the actual method is as follows.
    public String sendMessage(String sender, String recipient, String text){
    			HttpClient client = new HttpClient();
    			String url = "http://myapi.somewhere.com/http/sendmsg?";
    			String username = "xxx";
    			String password = "xxx";
    			String api_id = "xxx";
    			String status = "";
    			url = url + "user=" + username + "&password=" + password + "&api_id" + api_id;
    			url = url + "&to=" + recipient + "&from=" + sender + "&text=" + text;
    			GetMethod method = new GetMethod(url);
    		
    			DefaultMethodRetryHandler retryhandler = new DefaultMethodRetryHandler();
    			retryhandler.setRequestSentRetryEnabled(false);
    			retryhandler.setRetryCount(3);
    			method.setMethodRetryHandler(retryhandler);
    		
    			try {
    				  // Execute the method.
    				  int statusCode = client.executeMethod(method);
    
    				  if (statusCode != HttpStatus.SC_OK) {
    					System.err.println("Method failed: " + method.getStatusLine());
    				  }
    
    				  // Read the response body.
    				  byte[] responseBody = method.getResponseBody();
    
    				  // Deal with the response.
    				  // Use caution: ensure correct character encoding and is not binary data
    				  status = new String(responseBody);
    
    				} catch (IOException e) {
    				  System.err.println("Failed to download file.");
    				  e.printStackTrace();
    				} finally {
    				  // Release the connection.
    				  method.releaseConnection();
    				}
    				return status;
    		}
    

    Any ideas what's wrong?


  • Closed Accounts Posts: 25 dan_pretty_boy


    Hi ballooba,

    if you get a MessageManagerBean not Bound error. It means the bean is not tied to a jndi context..


    Is there an exception being thrown ??? Best bet is to have System.out's line at specfic places in your call stack and see where it goes..


    Regards
    Danny


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Your code above uses a Get method? Should you not be using the Post method instead?


  • Advertisement
  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Ok,

    Thanks for all your help so far.

    It now transpires that it doesn't seem to recognise the commons package at all.

    I made a JSP called 'testgateway.jsp' and ran it. I got a compile error stating that it the commons package does not exist.

    This has always been a mystery to me. Where do you put new packages to make them available?

    From what you said originally lynchie I thought that I had to put them on the classpath as specified in my Environment variables. however this does not appear to be working. This is beginning to wreck my head.

    Maybe I should have been a botanist or something.


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    Ok, First of all, jakarta comons is a set of common libraries that can be reused. Some of them have inter-dependencies. From looking at httpclient, it also requires commons-logging. So download it too.

    Secondly, to make new packages be available as you put it, you need to ensure that they are in the classpath when BOTH compiling and running your app. If you are doing everything from the command line make sure you specify the full path to the jar files in your -classpath argument. In an IDE such as eclipse, make sure you add it under Project Proeprties -> Libraries -> Add External Jar


  • Registered Users Posts: 6,315 ✭✭✭ballooba


    Thanks for all your help lynchie.

    This has been a total cluster fúck. All packages necessary are now available (I also need a codec package).

    The whole thing is finally working. My whole FYP depended on this so this is a big deal.

    Pretty satisfying when you actually get it!


Advertisement