Connecting standalone client to Stateless SessionBean in Glassfish 3 - java-ee-6

I've followed the instructions here to create a client to a remote SessionBean. I run the client on the same machine that Glassfish 3.1.2 beta is running on. When I use the gf-client.jar from the 3.1.2 beta Glassfish I get the following Exception which is the same Exception if I leave the gf-client.jar out of the classpath:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
If I use a 3.1.1 gf-client.jar from a Maven repository I get a huge stack trace with complaints about it not being able to find some .jar files from Derby which I'm not even using. Apparently a version mismatch problem.
Has anyone gotten a standalone client to connect to Glassfish 3.1.2 beta? Did this change in JavaEE 6?
Here's the code:
#Stateless
public class LockTestDeadlockService implements LockTestDeadlockServiceI {
public int getP1Id() throws SQLException {
int parentId = -1;
return parentId;
}
}
#Remote
public interface LockTestDeadlockServiceI {
public int getP1Id() throws SQLException;
public void insertChildUpdateParent(int parentId) throws SQLException;
}
Here's my client:
public class LoadTestClient {
static Logger logger = Logger.getLogger(LoadTestClient.class);
public static void main(String[] args) {
String jndiName = "java:global/locktest-0.0.1-SNAPSHOT/LockTestDeadlockService";
try {
LockTestDeadlockServiceI lockTestService =
(LockTestDeadlockServiceI) new InitialContext().lookup(jndiName);
logger.info("Got lockTestService Remote Interface");
} catch (NamingException e) {
logger.info("Failed to get lockTestService Remote Interface: " + e);
}
}
}

The short answer is that to connect to GF 3.x from a client, you need a mini-glassfish install via the Application Client Container (ACC) using either webstart or the package-appclient script. Open up the gf-client.jar and look at its classpath in the manifest file. There are a ton of files listed in there. This was similar in GF 2.x, but it seemed to need less dependencies on the client (though it was 15MB with that version).
See these:
Create an "Application Client" with Maven in Java EE
With which maven dependencies can i create a standalone JMS client for Glassfish?
http://docs.oracle.com/cd/E18930_01/html/821-2418/beakt.html#scrolltoc
http://docs.oracle.com/cd/E18930_01/html/821-2418/beakv.html#beakz

Related

Springboot postgres Failed to determine a suitable driver class

I am trying to develop web application using SpringBoot and Postgres Database. However, on connecting to the application, I am getting error "Failed to determine a suitable driver class"
As per advise in older posts, I have tried using driver of different version of jdbc and also tried creating bean for NamedParameterJdbcTemplate manually. I also validated that libraries are present and is accessible from Java code and those are present in classpath. But its still giving the same issue.
I am using gradle to import all jars into build path.
Here is the git repository for the code:
https://github.com/ashubisht/sample-sbs.git
Gradle dependency code:
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-websocket")
compile("org.springframework.boot:spring-boot-starter-jdbc")
//compile("org.postgresql:postgresql")
compile("org.postgresql:postgresql:9.4-1206-jdbc42")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Code for building Bean
#Configuration
#PropertySource("classpath:application.properties")
public class Datasource {
#Value("${db.driverClassName}")
private String driverClass;
#Value("${db.url}")
private String url;
#Value("${db.username}")
private String username;
#Value("${db.password}")
private String password;
#Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() throws Exception{
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(source);
return namedParameterJdbcTemplate;
}
}
Here is application.properties
server.port=8086
#spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
#spring.datasource.username=postgres
#spring.datasource.password=password
#spring.datasource.platform=postgresql
#spring.jpa.hibernate.ddl-auto=create-drop
db.driverClassName=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/testdb
db.username=postgres
db.password=password
The issue is resolved by creating two beans. Separate bean is created for DataSource and NamedParameterJdbcTemplate.
#Bean
public DataSource dataSource(){
System.out.println(driverClass+" "+ url+" "+username+" "+password);
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(driverClass);
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
return source;
}
#Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(this.dataSource());
return namedParameterJdbcTemplate;
}
For me the issue was in a miss-spell for postgresSql
its only one s,
replace
spring.datasource.url=jdbc:postgres://localhost:5432/databaseName
spring.datasource.url=jdbc:postgressql://localhost:5432/databaseName
with
spring.datasource.url=jdbc:postgresql://localhost:5432/databaseName
also check the same thing on hibernate dialect,
replace PostgresSQLDialect
with PostgreSQLDialect
Had the same problem.
The solution for me was to change application.properties file extension into application.yml
For me the error was
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put
it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
and the issue was missing profile
so I added the following in the classpath and it worked
spring.profiles.active=dev
Please try it
spring.r2dbc.url=r2dbc:postgresql://ip:port/datafeed?currentSchema=user_management
spring.r2dbc.username=username
spring.r2dbc.password=12345
spring.r2dbc.driver=postgresql
Hope to help you!
I got the same error. It happens when you install sts version 3.
I found the solution to this problem by doing trial & error method.
This error is occured due to the non-availability of the connection between Application Properties & the server. I got to know by changing the port number in the application Properties to 9090, later then while running the application the console showed the default port number 8080.
Thus you should maven clean and maven build your Spring Boot Application.
After the above step, you run your application normally as spring boot application, the database will get connected and the application will get started.

Spring Boot Admin Client not registering an application

I have setup a SpringBootAdmin server, and trying to register an application with SpringBootAdmin client. It does not seem to be registering.
Do I neccesarily have to register with Eureka?
How do I debug?
Configuration on adminserver
build.gradle
dependencies {
compile('de.codecentric:spring-boot-admin-server-ui')
compile('de.codecentric:spring-boot-admin-server-ui-login')
compile('de.codecentric:spring-boot-admin-server')
compile('org.springframework.boot:spring-boot-starter-web-services')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.properties
spring.application.name=Boot-Admin
server.port=8093
security.user.name=admin
security.user.password=admin
logging.level.de.codecentric.boot.admin.client=DEBUG
logging.level.de.codecentric.boot.admin=DEBUG
App is
#SpringBootApplication
#Configuration
#EnableAdminServer
public class AdminApp {
public static void main(String[] args) {
SpringApplication.run(AdminApp.class, args);
}
}
On the client side,
build.gradle
dependencies {
compile('de.codecentric:spring-boot-admin-starter-client')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web-services')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.properties
server.port=8091
spring.boot.admin.client.enabled=true
spring.boot.admin.url=http://localhost:8093
spring.boot.admin.client.auto-registration=true
spring.boot.admin.username=admin
spring.boot.admin.password=admin
logging.level.de.codecentric.boot.admin.client=DEBUG
Code is
#Configuration
#SpringBootApplication
public class SBACApp {
public static void main(String[] args) {
SpringApplication.run(SBACApp.class, args);
}
}
According to all the Stackoverflow articles and tutorials, this should be adequate.
Even though logging is set on the client side, there seems to be no log-line starting with d.c....
What could I be missing
Any additional knowledge on how to debug this may help.
If you are using spring boot admin 2.0 the the client url property would be
spring.boot.admin.client.url: http://localhost:8093
I would check to see what version you are using and then double check the property names.
In Spring Boot 2.x.x version client url property is different that Spring Boot 1.x.x
spring.boot.admin.client.enabled=true
spring.boot.admin.client.url= http://localhost:8093
spring.boot.admin.client.auto-registration=true
make sure you have other two properties. in my case i was missing auto registration property

java ee lookup ejbs from from different app

Java EE Tutorial is not helpful at all. Internet search was underwhelming.
I have an EJB module that is deployed to glassfish by itself. It has #Local and #Remote annotated iterfaces which are both implemented by the concrete class.
Then i have a REST resource that needs to get a reference to that ejb module and invoke some methods.
Can you give me a barebones, simple example of how that is done? I mean, i can't even inject SessionContext into my rest app, as it crashes... Please, keep it simple.
The ejb should just have a:
public String getMsg(){
return "ohai";
}
The rest service:
#GET
#Produces("text/plain")
public String asd(){
return <the myterious ejb that was injected somehow>.getMsg();
}
Thanks.
Alright, i figured it out. Using NetBeans, but probably applicable to Eclipse. Server - glassfish
Create webapp, an EJB -> call EJB from webapp. All these run inside the same server as separate modules.
First: create an EJB module, it will be deployed on its own:
remote interface:
package main;
import javax.ejb.Remote;
#Remote
public interface YourRemoteInterface{
public String tellMeSomething();
public void otherMethod(); //etc...
}
then create the EJB implementation class:
concrete implementation
package main;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.EJB; //crucial to JNDI lookup
#Remote(RemoteInterface.class)
#Stateless
#EJB(name="java:global:/MYSTUFF", beanInterface=YourRemoteInterface.class)
public class YourConcreteClass implements YourRemoteInterface{
#Override
public String tellMeSomething(){//...} //and do the other methods
}
#EJB name attribute names your bean, that you will use to look it up. Can by any name. For ex: "some-name", or "java:global/YourConcreteClass"
Part two - webapp:
For web app i used a rest service, but surely can be another EJB or a SE client app. For SE client you'd need to set connection info, but that for another life.
#Path("/somePath")
public class Service{
#GET
#Produces("text/plain")
public String qwe(){
try{
javax.naming.InitialCOntext ic = new javax.naming.InitialContext();
YourRemoteInterface rb = (YourRemoteInterface)ic.lookup("java:global:/MYSTUFF");
return rb.tellMeSomething();
} catch (Exception ex) {
return "F*uck your life";
}
}
}
Now, from Project Properties of your webapp, you need to:
1) add the ejb jar file to Libraries so it shows in the Compile tab. I used the "Add project" button
2) Build -> Packaging: add the ejb jar file to WAR content. I used "Add file/folder", where i navigated to NetBeans projects / the EJB module / build / dist
note: you may experience an error when trying to deploy the ejb, or redeploy it. Error name is: java.lang.RuntimeException: Error while binding JNDI name main.RemoteInterface#main.RemoteInterface for EJB RemoteBean . Skipping the vague explanation, to cure it, you need to execute a command in glassfish:
asadmin set server.ejb-container.property.disable-nonportable-jndi-names="true"
Now, you can compile the webapp and deploy it. Should work.
At the end it's that simple. I swear i've eaten the WHOLE ejb section in glassfish tutorial and nowhere do they tell you this stuff. It's so annoying.

Where do I place jetty.xml

Where do I need to place jetty.xml, in case of embedded Jetty?
jetty-web is placed inside WEB-INF and it is loaded automatically. I tried to put there jetty.xml, but it failed to load (it does not see it). I am using mvn jetty:run to run the service.
Their WIKI example doesn't work? http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
public class FileServerXml {
public static void main(String[] args) throws Exception {
Resource fileserver_xml = Resource.newSystemResource("fileserver.xml");
XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
Server server = (Server)configuration.configure();
server.start();
server.join();
}
}
It sounds like you need to restructure your jetty.xml in any event.

Where do I put my jetty.xml file with Jetty embedded?

I am just getting started with Jetty (Jetty 6 w/ Java 6). Using the example files with Jetty 6, I place my xml configuration file. in the same directory as my java file. But when I run the project I get this error.
Exception in thread "main" java.lang.NullPointerException at net.test.FileServerXml.main(FileServerXml.java:13
Here is the example code:
`package net.test;
import org.mortbay.jetty.Server;
import org.mortbay.resource.Resource;
import org.mortbay.xml.XmlConfiguration;
public class FileServerXml
{
public static void main(String[] args) throws Exception
{
Resource fileserver_xml = Resource.newSystemResource("fileserver.xml");
XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
Server server = (Server)configuration.configure();
server.start();
server.join();
}
}
What is the proper way to structure the file system so that my xml file is found?
After doing some experimentation and heavy soul searching in the API for I changed:
Resource fileserver_xml = Resource.newSystemResource("fileserver.xml");
To this
Resource fileserver_xml = Resource.newResource("fileserver.xml");
Then placed the fileserver.xml outside of the "src" directory, which is the project root. Then it worked.