Peavey Peavey - 1 year ago 64
PHP Question

Using an SessionBean in a plain php script

I am looking for a way to replace select functionality in an existing application with SessionBeans, but I fail to find an example on how to access a SessionBean in a plain php class.

I have a super simple Singleton SessionBean from the examples:


namespace MyVendor\MyApp;

* @Singleton(name="MySingletonBean")
class MySingletonBean extends \Stackable
protected $counter = 0;

public function raiseMe()
return $this->counter++;

Implementation inside a Servlet is pretty straight forward and works like a charm, but I can't find any documentation on getting access to this in a plain php file outside a Servlet.

I'd expect it so be rather simple like:


use MyVendor\MyApp\MySingletonBean;

class IndexController extends App
* maybe some funky annotations...
* @var \MyVendor\MyApp\MySingletonBean
* @EnterpriseBean(name="MySingletonBean")
protected $mySingletonBean;

public function index() {

// and then servlet style...
echo $this->mySingletonBean->raiseMe();

// ... or like this?
echo MySingletonBean::getInstance()->raiseMe();

Can someone please point me into the right direction?

Thank you,


Answer Source

As SessionBeans exists as real PHP objects inside the container it is not possible to access them from e. g. a PHP-FPM application. To do so, we've implemented a client, that returns a proxy to the session bean and allows you to execute it's methods. Additional it is necessary to configure the persistence container itself, because by default, we've deactivated remote connections for security reasons.

So, the first thing you should do, is to install the appserver-io/rmi package as Composer requirement in your project, e. g.

$ composer install appserver-io/rmi

after that, you've to configure a server, that allows remote access to the persistence container, e. g. you can add the following XML snippet to the <container name="combined-appserver"> in etc/appserver/appserver.xml file


    <param name="admin" type="string"></param>
    <param name="transport" type="string">tcp</param>
    <param name="address" type="string"></param>
    <param name="port" type="integer">8585</param>
    <param name="workerNumber" type="integer">4</param>
    <param name="workerAcceptMin" type="integer">3</param>
    <param name="workerAcceptMax" type="integer">8</param>
    <param name="documentRoot" type="string">webapps</param>
    <param name="directoryIndex" type="string">index.pc</param>
    <param name="keepAliveMax" type="integer">64</param>
    <param name="keepAliveTimeout" type="integer">5</param>
    <param name="errorsPageTemplatePath" type="string">resources/templates/www/error.phtml</param>

    <environmentVariable condition="" definition="LOGGER_ACCESS=Access" />

    <connectionHandler type="\AppserverIo\WebServer\ConnectionHandlers\HttpConnectionHandler" />
    <!-- connectionHandler type="\AppserverIo\Appserver\MessageQueue\ConnectionHandlers\AmqpConnectionHandler" / -->

    <!-- per default allow everything -->
    <access type="allow">
            <param name="X_REQUEST_URI" type="string">.*</param>

    <!-- REQUEST_POST hook -->
    <module type="\AppserverIo\WebServer\Modules\AuthenticationModule"/>
    <module type="\AppserverIo\WebServer\Modules\VirtualHostModule"/>
    <module type="\AppserverIo\WebServer\Modules\EnvironmentVariableModule" />
    <module type="\AppserverIo\WebServer\Modules\RewriteModule"/>
    <module type="\AppserverIo\WebServer\Modules\DirectoryModule"/>
    <module type="\AppserverIo\WebServer\Modules\AccessModule"/>
    <module type="\AppserverIo\WebServer\Modules\CoreModule"/>
    <module type="\AppserverIo\Appserver\PersistenceContainer\PersistenceContainerModule" />
    <!-- RESPONSE_PRE hook -->
    <module type="\AppserverIo\WebServer\Modules\DeflateModule"/>
    <!-- RESPONSE_POST hook -->
    <!-- module type="\AppserverIo\Appserver\Core\Modules\ProfileModule"/ -->

    <fileHandler name="persistence-container" extension=".pc" />


below the message-queue server configuration.

Finally you can connect to the persistence container and call the raiseCounter() method of the ASingletonProcessor SessionBean of our example application with


use AppserverIo\RemoteMethodInvocation\RemoteConnectionFactory;

require_once __DIR__ . '/vendor/autoload.php';

$connection = RemoteConnectionFactory::createContextConnection();

$session = $connection->createContextSession();
$proxy = $session->createInitialContext()->lookup('ASingletonProcessor');

echo $proxy->raiseCounter() . PHP_EOL;

The example uses a dummy Session-ID generated with md5('test) which is for testing purposes only. In a real life application you will use the default PHP Session-ID for example.

Hope that give's you an good impression how things could go :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download