I am looking for a way to replace select functionality in an existing application with appserver.io 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:
<?php
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:
<?php
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,
Peter
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
<server
name="persistence-container"
type="\AppserverIo\Server\Servers\MultiThreadedServer"
worker="\AppserverIo\Server\Workers\ThreadWorker"
socket="\AppserverIo\Server\Sockets\StreamSocket"
requestContext="\AppserverIo\Server\Contexts\RequestContext"
serverContext="\AppserverIo\Appserver\Server\Contexts\StandardServerContext"
streamContext="\AppserverIo\Server\Contexts\StreamContext"
loggerName="System">
<params>
<param name="admin" type="string">info@appserver.io</param>
<param name="transport" type="string">tcp</param>
<param name="address" type="string">127.0.0.1</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>
</params>
<environmentVariables>
<environmentVariable condition="" definition="LOGGER_ACCESS=Access" />
</environmentVariables>
<connectionHandlers>
<connectionHandler type="\AppserverIo\WebServer\ConnectionHandlers\HttpConnectionHandler" />
<!-- connectionHandler type="\AppserverIo\Appserver\MessageQueue\ConnectionHandlers\AmqpConnectionHandler" / -->
</connectionHandlers>
<accesses>
<!-- per default allow everything -->
<access type="allow">
<params>
<param name="X_REQUEST_URI" type="string">.*</param>
</params>
</access>
</accesses>
<modules>
<!-- 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"/ -->
</modules>
<fileHandlers>
<fileHandler name="persistence-container" extension=".pc" />
</fileHandlers>
</server>
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
<?php
use AppserverIo\RemoteMethodInvocation\RemoteConnectionFactory;
require_once __DIR__ . '/vendor/autoload.php';
$connection = RemoteConnectionFactory::createContextConnection();
$connection->injectPort(8585);
$connection->injectAddress('127.0.0.1');
$connection->injectTransport('http');
$connection->injectAppName('example');
$session = $connection->createContextSession();
$session->setSessionId(md5('test'));
$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 :)