Since TYPO3 12.2, developers can leverage the powerful Symfony Messenger component integrated directly into the TYPO3 core. This game-changing feature enables asynchronous processing of time-consuming tasks, improving user experience and system performance.
What is Symfony Messenger in TYPO3?
TYPO3's message bus solution is based on symfony/messenger and can send messages to be handled immediately (synchronous) or through transports like queues (asynchronous) to be handled later. For backwards compatibility, the default implementation uses the synchronous transport.
Why Use Background Jobs in TYPO3?
Background jobs are essential for:
- Email processing - Send newsletters without blocking the frontend
- Image optimization - Process uploaded images asynchronously
- Data imports - Handle large CSV imports efficiently
- Report generation - Create complex reports without timeouts
- Cache warming - Rebuild caches in the background
Setting Up Symfony Messenger in TYPO3
Step 1: Configure Message Transport
Create or modify your config/system/additional.php:
<?php
return [
'EXTENSIONS' => [
'messenger' => [
'transports' => [
'async' => [
'dsn' => 'doctrine://default',
'options' => [
'table_name' => 'messenger_messages',
'queue_name' => 'async'
]
]
]
]
]
];
Step 2: Create a Message Class
Create a simple message class in your extension:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Message;
final class ProcessImageMessage
{
public function __construct(
private readonly int $fileId,
private readonly string $processingType = 'thumbnail'
) {}
public function getFileId(): int
{
return $this->fileId;
}
public function getProcessingType(): string
{
return $this->processingType;
}
}
Step 3: Create a Message Handler
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\MessageHandler;
use MyVendor\MyExtension\Message\ProcessImageMessage;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
final class ProcessImageMessageHandler
{
public function __invoke(ProcessImageMessage $message): void
{
// Your background processing logic here
$fileId = $message->getFileId();
$processingType = $message->getProcessingType();
// Process the image
$this->processImage($fileId, $processingType);
}
private function processImage(int $fileId, string $type): void
{
// Image processing implementation
// This runs in the background!
}
}
Step 4: Register the Handler
Add to your Configuration/Services.yaml:
services:
MyVendor\MyExtension\MessageHandler\ProcessImageMessageHandler:
tags:
- name: messenger.message_handler
handles: MyVendor\MyExtension\Message\ProcessImageMessage
Step 5: Configure Routing
Add routing configuration to send messages to the async transport:
framework:
messenger:
routing:
'MyVendor\MyExtension\Message\ProcessImageMessage': async
Dispatching Messages
In your controller or service, dispatch messages to the queue:
<?php
use TYPO3\CMS\Core\Messaging\MessageBus;
use MyVendor\MyExtension\Message\ProcessImageMessage;
class ImageController
{
public function __construct(
private readonly MessageBus $messageBus
) {}
public function uploadAction(): void
{
// Handle file upload
$fileId = 123;
// Dispatch to background queue
$this->messageBus->dispatch(
new ProcessImageMessage($fileId, 'thumbnail')
);
// User sees immediate response
// Processing happens in background
}
}
Processing the Queue
The queue system includes an asynchronous mode to handle time-consuming tasks more efficiently. Run the worker to process queued messages:
# Process messages from the async transport
php bin/console messenger:consume async
# Process with limits
php bin/console messenger:consume async --limit=10 --time-limit=3600
Best Practices for TYPO3 Background Jobs
1. Error Handling Always implement proper error handling in your message handlers to prevent infinite retry loops.
2. Resource Management Monitor memory usage and processing time to avoid overwhelming your server.
3. Monitoring Use the messenger:stats command to monitor queue health and message counts.
4. Scaling Run multiple workers for high-volume applications or use external queue systems like Redis or RabbitMQ.
Conclusion
With TYPO3 12 the powerful symfony/messenger component has been integrated into the TYPO3 core, enabling developers to build more responsive applications. By implementing background jobs for time-consuming tasks, you can significantly improve user experience while maintaining system performance.
Start small with simple tasks like email processing, then gradually move complex operations to background queues. Your users will appreciate the faster response times, and your server will thank you for the reduced load.
The asynchronous future of TYPO3 development is here – embrace it to build better, more scalable applications!
Post a Comment