SSL with Grizzly and Jersey - java

I'm trying to get grizzly to use SSL encryption and still work fine with Jersey. I've looked all over the Internet, and I find all kinds of different attempts at SSL with Grizzly and Jersey. Seems like there are different ways of doing it depending on which version you are using, and how you decided to implement it. I haven't been able to get any examples to work with my code yet.
Here's how I start up my server:
static HttpServer startSecureServer() throws IOException{
ResourceConfig rc=new PackagesResourceConfig("server.grizzlyresources");
SSLContextConfigurator sslCon=new SSLContextConfigurator();
sslCon.setKeyStoreFile(ConfigLoader.getKeystoreLocation()); // contains server keypair
sslCon.setKeyStorePass(ConfigLoader.getKeystorePassword());
System.out.println("Starting server on port "+ConfigLoader.getHttpsServerPort());
HttpServer secure=GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED, rc);
secure.stop();
HashSet<NetworkListener> lists=new HashSet<NetworkListener>(secure.getListeners());
for (NetworkListener listener : lists){
listener.setSecure(true);
SSLEngineConfigurator ssle=new SSLEngineConfigurator(sslCon);
listener.setSSLEngineConfig(ssle);
secure.addListener(listener);
System.out.println(listener);
}
secure.start();
return secure;
}
private static URI getBaseURISecured(){
return UriBuilder.fromUri("https://0.0.0.0/").port(ConfigLoader.getHttpsServerPort()).build();
}
private static final URI BASE_URI_SECURED = getBaseURISecured();
ConfigLoader loads in information from a config file. When I run this code, it starts up the server, it finds the resources in the server.grizzlyresources package, and it works great! Except for one thing. The server isn't secured. I can telnet into it and send an HTTP request in plain text for one of my resources, and it will return it. So the code works for starting up the server, but the whole SSL part of it is just being bypassed. Any ideas how to fix this or why it might be doing this?
Here's the output to the console when I run it:
Starting server on port 9999
Jan 13, 2014 9:51:08 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
server.grizzlyresources
Jan 13, 2014 9:51:08 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class server.grizzlyresources.SessionResource
class server.grizzlyresources.LoginResource
Jan 13, 2014 9:51:08 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Jan 13, 2014 9:51:08 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM'
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:9999]
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.NetworkListener stop
INFO: Stopped listener bound to [0.0.0.0:9999]
NetworkListener{name='grizzly', host='0.0.0.0', port=9999, secure=true}
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:9999]
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
I'm using Grizzly 2.2.1, and Jersey 1.12.
Thanks a bunch!

Sorry to take so long to post this up here. Alexey's answer led me to the working solution, which is a lot like Wolfgang Fahl's code. Here's what I ended up with:
static HttpServer startSecureServer() throws IOException
{
System.out.println("Starting server on port " + ConfigLoader.getHttpsServerPort());
ResourceConfig rc = new PackagesResourceConfig("com.kinpoint.server.grizzlyresources");
SSLContextConfigurator sslCon = new SSLContextConfigurator();
sslCon.setKeyStoreFile(ConfigLoader.getKeystoreLocation()); // contains server keypair
sslCon.setKeyStorePass(ConfigLoader.getKeystorePassword());
HttpHandler hand = ContainerFactory.createContainer(HttpHandler.class, rc);
HttpServer secure = GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED, hand, true,
new SSLEngineConfigurator(sslCon, false, false, false));
return secure;
}
The second parameter in the SSLEngineConfigurator tells it not to use client mode. That was what was messing me up. Thanks for the help.

IMO you can use different Factory method to initialize secured Grizzly HttpServer:
HttpServer secure = GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED,
ContainerFactory.createContainer(HttpHandler.class, rc),
true,
new SSLEngineConfigurator(sslCon));
If you initialize the server like this, you don't need to stop and reconfigure it again.
Hope this will help.

I have a nice and tested example using Grizzly 2.3.3 in:
https://github.com/danielnuriyev/scriptedstuff/tree/master/src/com/scriptedstuff/server

The following code works with Grizzly 2.3.7 and I am using Jersey 1.18 - this includes code for SSL Client Authentication - if you don't have the keystores this feature will simply be ignored.
/**
* create a Server based on an url and possibly a ResourceConfig
*
* #param url
* #param rc
* #param secure
* - true if SSL should be used
* #param contextPath
* #return
* #throws Exception
*/
public HttpServer createHttpServer(String url, ResourceConfig rc,
boolean secure, String contextPath) throws Exception {
// HttpServer result = GrizzlyServerFactory.createHttpServer(url, rc);
// http://grepcode.com/file/repo1.maven.org/maven2/com.sun.jersey/jersey-grizzly2/1.6/com/sun/jersey/api/container/grizzly2/GrizzlyServerFactory.java#GrizzlyServerFactory.createHttpServer%28java.net.URI%2Ccom.sun.jersey.api.container.grizzly2.ResourceConfig%29
HttpServer result = new HttpServer();
final NetworkListener listener = new NetworkListener("grizzly",
settings.getHost(), settings.getPort());
result.addListener(listener);
// do we need SSL?
if (secure) {
listener.setSecure(secure);
SSLEngineConfigurator sslEngineConfigurator = createSSLConfig(true);
listener.setSSLEngineConfig(sslEngineConfigurator);
}
// Map the path to the processor.
final ServerConfiguration config = result.getServerConfiguration();
final HttpHandler handler = ContainerFactory.createContainer(
HttpHandler.class, rc);
config.addHttpHandler(handler, contextPath);
return result;
}
/**
* create SSL Configuration
*
* #param isServer
* true if this is for the server
* #return
* #throws Exception
*/
private SSLEngineConfigurator createSSLConfig(boolean isServer)
throws Exception {
final SSLContextConfigurator sslContextConfigurator = new SSLContextConfigurator();
// override system properties
final File cacerts = getStoreFile("server truststore",
"truststore_server.jks");
if (cacerts != null) {
sslContextConfigurator.setTrustStoreFile(cacerts.getAbsolutePath());
sslContextConfigurator.setTrustStorePass(TRUSTSTORE_PASSWORD);
}
// override system properties
final File keystore = getStoreFile("server keystore", "keystore_server.jks");
if (keystore != null) {
sslContextConfigurator.setKeyStoreFile(keystore.getAbsolutePath());
sslContextConfigurator.setKeyStorePass(TRUSTSTORE_PASSWORD);
}
//
boolean clientMode = false;
// force client Authentication ...
boolean needClientAuth = settings.isNeedClientAuth();
boolean wantClientAuth = settings.isWantClientAuth();
SSLEngineConfigurator result = new SSLEngineConfigurator(
sslContextConfigurator.createSSLContext(), clientMode, needClientAuth,
wantClientAuth);
return result;
}

Related

Error writing to S3 using TextIO.write() (Google Dataflow)

I have a Google Dataflow code similar to the below
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.io.FileSystems;
import org.apache.beam.sdk.io.TextIO;
import org.apache.beam.sdk.io.fs.ResourceId;
import org.apache.beam.sdk.options.*;
import org.apache.beam.sdk.transforms.Create;
public class S3Test {
public static void main(String[] args) {
PipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().create();
Pipeline pipeline = Pipeline.create(options);
ResourceId outputDir = FileSystems.matchNewResource("s3://my-bucket/temp/", true);
pipeline
.apply("New",
Create.of("Hello World!!")
)
.apply(
"Write to S3",
TextIO.write()
.to("s3://my-bucket/test.txt")
.withoutSharding()
.withTempDirectory(outputDir)
);
pipeline.run();
}
}
This works to some extent, in that it first creates a temporary file on my S3 bucket. However, when it tries to rename it to my final desired/input filename, it throws the following error.
Receiver class org.apache.beam.sdk.io.aws.s3.S3FileSystem does not
define or inherit an implementation of the resolved method 'abstract
void rename(java.util.List, java.util.List,
org.apache.beam.sdk.io.fs.MoveOptions[])' of abstract class
org.apache.beam.sdk.io.FileSystem.
I guess I have to override or implement a rename() function, but not sure where I'd start on that.
Has anyone tried to write successfully to S3 using TextIO.write()?
EDIT:
Below is the full message in the Run window:
Dec 24, 2022 4:56:34 PM
org.apache.beam.sdk.io.WriteFiles$WriteShardsIntoTempFilesFn
processElement INFO: Opening writer
8a0dbf70-a0fc-48dd-92f9-8ff4000a8076 for window
org.apache.beam.sdk.transforms.windowing.GlobalWindow#2dddc1b9 pane
PaneInfo{isFirst=true, isLast=true, timing=ON_TIME, index=0,
onTimeIndex=0} destination null Dec 24, 2022 4:56:37 PM
org.apache.beam.sdk.io.FileBasedSink$Writer close INFO: Successfully
wrote temporary file
s3://my-bucket/temp/.temp-beam-13da7e3e-15a9-4285-a90c-30c7c6dcfe99/9e53ec6a8a0dbf70-a0fc-48dd-92f9-8ff4000a8076
Dec 24, 2022 4:56:37 PM
org.apache.beam.sdk.io.WriteFiles$FinalizeTempFileBundles$FinalizeFn
process INFO: Finalizing 1 file results Dec 24, 2022 4:56:37 PM
org.apache.beam.sdk.io.FileBasedSink$WriteOperation
createMissingEmptyShards INFO: Finalizing for destination null num
shards 1. Dec 24, 2022 4:56:37 PM
org.apache.beam.sdk.io.FileBasedSink$WriteOperation moveToOutputFiles
INFO: Will copy temporary file
FileResult{tempFilename=s3://my-bucket/temp/.temp-beam-13da7e3e-15a9-4285-a90c-30c7c6dcfe99/9e53ec6a8a0dbf70-a0fc-48dd-92f9-8ff4000a8076,
shard=0,
window=org.apache.beam.sdk.transforms.windowing.GlobalWindow#2dddc1b9,
paneInfo=PaneInfo{isFirst=true, isLast=true, timing=ON_TIME, index=0,
onTimeIndex=0}} to final location s3://my-bucket/test.txt Exception in
thread "main" org.apache.beam.sdk.Pipeline$PipelineExecutionException:
java.lang.AbstractMethodError: Receiver class
org.apache.beam.sdk.io.aws.s3.S3FileSystem does not define or inherit
an implementation of the resolved method 'abstract void
rename(java.util.List, java.util.List,
org.apache.beam.sdk.io.fs.MoveOptions[])' of abstract class
org.apache.beam.sdk.io.FileSystem. at
org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish(DirectRunner.java:374)
at
org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish(DirectRunner.java:342)
at
org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:218)
at
org.apache.beam.runners.direct.DirectRunner.run(DirectRunner.java:67)
at org.apache.beam.sdk.Pipeline.run(Pipeline.java:323) at
org.apache.beam.sdk.Pipeline.run(Pipeline.java:309) at
com.example.S3Test.main(S3Test.java:255) Caused by:
java.lang.AbstractMethodError: Receiver class
org.apache.beam.sdk.io.aws.s3.S3FileSystem does not define or inherit
an implementation of the resolved method 'abstract void
rename(java.util.List, java.util.List,
org.apache.beam.sdk.io.fs.MoveOptions[])' of abstract class
org.apache.beam.sdk.io.FileSystem. at
org.apache.beam.sdk.io.FileSystems.renameInternal(FileSystems.java:323)
at org.apache.beam.sdk.io.FileSystems.rename(FileSystems.java:308)
at
org.apache.beam.sdk.io.FileBasedSink$WriteOperation.moveToOutputFiles(FileBasedSink.java:802)
at
org.apache.beam.sdk.io.WriteFiles$FinalizeTempFileBundles$FinalizeFn.process(WriteFiles.java:1077)
Try updating your pipeline... by default withNumShards() is set to zero, so it will write to a single file
pipeline
.apply("New",
Create.of("Hello World!!")
)
.apply(
"Write to S3",
TextIO.write()
.to("s3://my-bucket/test.txt")
.withNumShards(1)
.withTempDirectory(outputDir)
);

Trouble Connecting to MongoDB with Java Async Client

I've used MongoDB with Python for a while now, and I've recently tried it with Java and i keep getting the same error.
Here is my code (with username and password replaced):
public class MongoClientConnection {
public static void main(String[] args) {
MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>#cluster0.bog1c.mongodb.net/db?w=majority");
MongoDatabase database = mongoClient.getDatabase("db");
MongoCollection<Document> accounts = database.getCollection("accounts");
Document doc = new Document("name", new String[]{"John", "Smith"})
.append("age", 35)
.append("favorite_color", "blue");
accounts.insertOne(doc, new SingleResultCallback<Void>() {
#Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Inserted!");
}
});
}
}
This was taken directly from this tutorial.
Here is the output:
Jan 15, 2022 3:17:53 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[cluster0.bog1c.mongodb.net:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Jan 15, 2022 3:17:53 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=cluster0.bog1c.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Process finished with exit code 0
I am able to connect to the database using Mongosh. The string given on the cloud.mongodb.com site begins with mongodb+srv://, however it gives me the following error:
Exception in thread "main" java.lang.IllegalArgumentException: uri needs to start with mongodb://
That is why I am using only mongodb://.
I am running the code on Intellij IDEA in Windows.
Any ideas for what I am doing wrong?

Java rest server : make a unit test

I try to make a unit test for a standalone rest server. If I run the rest server it work nice. But I don't know how to make the UnitTest running.
My main class :
public class Main {
private static final int DEFAULT_PORT = 8080;
private final int serverPort;
private final Server restServer;
public Main(final int serverPort) throws Exception {
this.serverPort = serverPort;
restServer = configureServer();
restServer.start();
restServer.join();
}
public void close() throws Exception {
if (restServer != null) {
restServer.stop();
}
}
private Server configureServer() {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.packages(Main.class.getPackage().getName());
resourceConfig.register(JacksonFeature.class);
ServletContainer servletContainer = new ServletContainer(resourceConfig);
ServletHolder sh = new ServletHolder(servletContainer);
Server server = new Server(serverPort);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(sh, "/*");
server.setHandler(context);
return server;
}
public static void main(String[] args) throws Exception {
int serverPort = DEFAULT_PORT;
if (args.length >= 1) {
try {
serverPort = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
new Main(serverPort);
}
The resource class :
#Path("builder")
public class ReportBuilderResource {
#POST
#Path("/build")
#Consumes(MediaType.APPLICATION_JSON)
#Produces({MediaType.TEXT_PLAIN})
public String makeReport(final ReportDescription reportDescription) {
return reportDescription.getName();
}
}
My Unit test class :
public class ReportBuilderResourceTest extends JerseyTest {
#Override
public AppDescriptor configure() {
return new WebAppDescriptor.Builder()
.initParam(WebComponent.RESOURCE_CONFIG_CLASS, ClassNamesResourceConfig.class.getName())
.initParam(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, ReportBuilderResource.class.getName())
.build();
}
#Test
public void testBuildReport() throws Exception {
System.out.println("Test Build Report");
ReportDescription reportDescription = new ReportDescription();
JSONObject jsonObject = new JSONObject(reportDescription);
resource().path("builder/").post(jsonObject.toString());
}
And the output log :
juil. 31, 2015 9:48:53 AM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer <init>
INFO: Creating low level InMemory test container configured at the base URI http://localhost:9998/
Running com.fdilogbox.report.serveur.ReportBuilderResourceTest
juil. 31, 2015 9:48:53 AM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer start
INFO: Starting low level InMemory test container
juil. 31, 2015 9:48:53 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 03:25 AM'
Test Build Report
juil. 31, 2015 9:48:54 AM com.sun.jersey.api.container.filter.LoggingFilter filter
INFO: 1 * Server in-bound request
1 > POST http://localhost:9998/builder/
1 > Content-Type: text/plain
1 >
{"name":null,"report":null}
juil. 31, 2015 9:48:54 AM com.sun.jersey.api.container.filter.LoggingFilter$Adapter finish
INFO: 1 * Server out-bound response
1 < 405
1 < Allow: OPTIONS
1 <
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.497 sec <<< FAILURE! - in com.fdilogbox.report.serveur.ReportBuilderResourceTest
testBuildReport(com.fdilogbox.report.serveur.ReportBuilderResourceTest) Time elapsed: 0.496 sec <<< ERROR!
com.sun.jersey.api.client.UniformInterfaceException: Client response status: 405
at com.sun.jersey.api.client.WebResource.voidHandle(WebResource.java:709)
at com.sun.jersey.api.client.WebResource.post(WebResource.java:238)
at com.fdilogbox.report.serveur.ReportBuilderResourceTest.testBuildReport(ReportBuilderResourceTest.java:47)
I think the server is not "running" for the test. How can'I do this?
Your resource listens to "/builder", but the only method inside listens to "/builder/build". Since there is no method listening to #post and "/builder" you get a Http 405 - Method not allowed.
You can either remove #Path("/build") from the "makeReport" method, or change resource().path("builder/build")... in your test.
Btw:
You only need to add one of these contatiner adapters and this snippet to run a unit tests with Jersey 2:
public class ReportBuilderResourceTest extends JerseyTest {
#Override
protected Application configure() {
return new ResourceConfig(ReportBuilderResource.class);
}
...
}

Failed to open channel for context EJBReceiverContext

When I run the ejb3 remote client the service deployed in the server, WildFly8.1.0 Final I get the expected results. But now I am trying to benchmark this and when I run the client through the JMH #Benchmark method it runs fine for but after some iterations in warm up process it gives the following error.
Mar 04, 2015 12:29:15 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext#13689c
56, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection#25d8cd6c,channel=jboss.ejb,nodename=mast
er:server-three]} on channel Channel ID c1426f51 (outbound) of Remoting connection 3836a177 to /192.168.17.150:8330
Result is given by [ host: isurug ], [ Result: ONE ]
____ejb:poc_ear-1.0/poc_ejbs-1.0//DataGridServiceImpl!com.oms.ejbs.stateless.DataGridService
Mar 04, 2015 12:29:15 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
Mar 04, 2015 12:29:15 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext#538da6
b, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection#25d8cd6c,channel=jboss.ejb,nodename=maste
r:server-three]} on channel Channel ID bed866b3 (outbound) of Remoting connection 3836a177 to /192.168.17.150:8330
Result is given by [ host: isurug ], [ Result: ONE ]
____ejb:poc_ear-1.0/poc_ejbs-1.0//DataGridServiceImpl!com.oms.ejbs.stateless.DataGridService
Mar 04, 2015 12:29:15 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver$1 handleFailed
ERROR: Failed to open channel for context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext#62af85f9, receiver=Remoting connection EJB rece
iver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection#25d8cd6c,channel=jboss.ejb,nodename=master:server-three]}
org.jboss.remoting3.ProtocolException: Too many channels open
at org.jboss.remoting3.remote.RemoteConnectionHandler.handleOutboundChannelOpen(RemoteConnectionHandler.java:185)
at org.jboss.remoting3.remote.RemoteConnectionHandler.open(RemoteConnectionHandler.java:318)
at org.jboss.remoting3.ConnectionImpl.openChannel(ConnectionImpl.java:75)
at org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection.openChannel(ConnectionPool.java:227)
at org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver.associate(RemotingConnectionEJBReceiver.java:169)
at org.jboss.ejb.client.EJBClientContext.registerEJBReceiver(EJBClientContext.java:375)
at org.jboss.ejb.client.EJBClientContext.registerEJBReceiver(EJBClientContext.java:327)
at org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector.setupEJBReceivers(ConfigBasedEJBClientContextSelector.java:159)
at org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector.getCurrent(ConfigBasedEJBClientContextSelector.java:115)
at org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector.getCurrent(ConfigBasedEJBClientContextSelector.java:47)
at org.jboss.ejb.client.EJBClientContext.getCurrent(EJBClientContext.java:271)
at org.jboss.ejb.client.EJBClientContext.requireCurrent(EJBClientContext.java:281)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:176)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:144)
at com.sun.proxy.$Proxy1.getData(Unknown Source)
at com.oms.client.EJBInvoker.invokeDataGridService(EJBInvoker.java:71)
at com.test.MyBenchmark.testEjbClient(MyBenchmark.java:43)
at com.test.generated.MyBenchmark_testEjbClient.testEjbClient_avgt_jmhLoop(MyBenchmark_testEjbClient.java:160)
at com.test.generated.MyBenchmark_testEjbClient.testEjbClient_AverageTime(MyBenchmark_testEjbClient.java:129)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.openjdk.jmh.runner.LoopBenchmarkHandler$BenchmarkTask.call(LoopBenchmarkHandler.java:210)
at org.openjdk.jmh.runner.LoopBenchmarkHandler$BenchmarkTask.call(LoopBenchmarkHandler.java:192)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Error says too many channels open, so is it like I should close the channel after a call? Or what is the cause for this and what is the solution.
Following is my remote client.
public class EJBInvoker {
/**
* Invoke Data Grid Service.
*/
public void invokeDataGridService() {
EJBInvoker client = null;
try {
for (int i = 0; i < 1; i++) {
client = new EJBInvoker();
String dataGridServiceJndi = "ejb:poc_ear-1.0/poc_ejbs-1.0//DataGridServiceImpl!com.oms.ejbs.stateless.DataGridService";
DataGridService dataGridService = (DataGridService) client.lookupRemoteEJB(dataGridServiceJndi);
String result = dataGridService.getData("1");
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Lookup for Remote EJB
*
* #return {#link Object} instance.
* #throws NamingException
* naming exception.
*/
#SuppressWarnings({"unchecked", "rawtypes"})
private Object lookupRemoteEJB(String jndiName) throws NamingException {
Context context = null;
try {
Properties clientProperties = new Properties();
clientProperties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
clientProperties.put("remote.connections", "default");
clientProperties.put("remote.connection.default.port", "8330");
clientProperties.put("remote.connection.default.host", "192.168.17.150");//192.168.16.40
clientProperties.put("remote.connection.default.username", "admin");
clientProperties.put("remote.connection.default.password", "1qaz2wsx#");
clientProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration(clientProperties);
ContextSelector<EJBClientContext> contextSelector = new ConfigBasedEJBClientContextSelector(ejbClientConfiguration);
EJBClientContext.setSelector(contextSelector);
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
context = new InitialContext(properties);
System.out.println("____" + jndiName);
return context.lookup(jndiName);
} finally {
if (context != null) {
context.close();
}
}
}
}
We can't create too many InitialContext. In my application, I made InitialContext as a static variable; so in the whole app, only a instance.
That solved the issue.

How to make outbound calls in AsteriskNow using Asterisk AMI with Java

I would like to initiate a call from extension number to another in LAN network. I have done this task through FreePBX GUI. Now I am trying to execute this task by using Asterisk AMI with Java code. Below Java code shows some exception while in debugging.
import java.io.IOException;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.OriginateAction;
import org.asteriskjava.manager.response.ManagerResponse;
public class HelloManager
{
private ManagerConnection managerConnection;
public HelloManager() throws IOException
{
ManagerConnectionFactory factory = new ManagerConnectionFactory(
"192.168.68.173", "admin", "admin");
this.managerConnection = factory.createManagerConnection();
}
public void run() throws IOException, AuthenticationFailedException,
TimeoutException
{
OriginateAction originateAction;
ManagerResponse originateResponse;
originateAction = new OriginateAction();
originateAction.setChannel("SIP/1010");
originateAction.setContext("default");
originateAction.setExten("2020");
originateAction.setPriority(new Integer(1));
originateAction.setTimeout(new Integer(30000));
// connect to Asterisk and log in
managerConnection.login();
// send the originate action and wait for a maximum of 30 seconds for Asterisk
// to send a reply
originateResponse = managerConnection.sendAction(originateAction, 30000);
// print out whether the originate succeeded or not
System.out.println("Enter Response="+originateResponse.getResponse());
// and finally log off and disconnect
managerConnection.logoff();
}
public static void main(String[] args) throws Exception
{
HelloManager helloManager;
helloManager = new HelloManager();
helloManager.run();
}
}
Exception:
run:
Feb 06, 2014 4:04:16 PM org.asteriskjava.manager.internal.ManagerConnectionImpl connect
INFO: Connecting to 192.168.68.173:5038
Feb 06, 2014 4:04:17 PM org.asteriskjava.manager.internal.ManagerConnectionImpl setProtocolIdentifier
INFO: Connected via Asterisk Call Manager/1.3
Feb 06, 2014 4:04:17 PM org.asteriskjava.manager.internal.ManagerConnectionImpl setProtocolIdentifier
WARNING: Unsupported protocol version 'Asterisk Call Manager/1.3'. Use at your own risk.
Feb 06, 2014 4:04:18 PM org.asteriskjava.manager.internal.ManagerConnectionImpl disconnect
INFO: Closing socket.
Feb 06, 2014 4:04:18 PM org.asteriskjava.manager.internal.ManagerReaderImpl run
INFO: Terminating reader thread: No more lines available: null
Exception in thread "main" org.asteriskjava.manager.AuthenticationFailedException: Authentication failed
at org.asteriskjava.manager.internal.ManagerConnectionImpl.doLogin(ManagerConnectionImpl.java:578)
at org.asteriskjava.manager.internal.ManagerConnectionImpl.login(ManagerConnectionImpl.java:438)
at org.asteriskjava.manager.internal.ManagerConnectionImpl.login(ManagerConnectionImpl.java:423)
at org.asteriskjava.manager.DefaultManagerConnection.login(DefaultManagerConnection.java:294)
at asteriskjtapi.HelloManager.run(HelloManager.java:49)
at asteriskjtapi.HelloManager.main(HelloManager.java:67)
Note:
Here I am using the credentials in FreePBX. Is this right one to access ConnectionManager?
Kindly paste your asterisk log while executing your java program
For getting asterisk log
tail -f /var/log/asterisk/full
For checking the same from command line try this
telnet 192.168.68.173 5038
Action: Login
Username: admin
Secret: admin
if login sucessfull
Action: Originate
Channel: SIP/1010
Exten: 2020
Context: default
Priority: 1
Callerid: 1010
Async: yes
Try this and let me know your outputs
First of all, you have to try to connect with asterisk through telnet.
If you get 'Login success' message, you can proceed with your programming. Otherwise if you get an 'Authentication failed' message, add your ip to the permit setting in manager.conf. Then save the changes.
permit:[your static ip]
Try again with telnet

Categories

Resources